Thursday, December 18, 2008

Arrays in C

Arrays-
This concept is used to to store group of data. If many variables are to be accepted then arrays are used. Arrays are used because for accepting many variables that many variables should be declared. This is not convenient. The arrays are similar to arrays in mathematics inpresentation. There are three types of arrays.
1. Single dimension arrays.
2. Two dimension arrays.
3. Multi dimension arrays.
Let us see first the declaration of array. We will see how single dimension arrays are defined. Similarly the two and three dimension arrays are used.
1. Declaration of single dimension array-
Integer array-int x[5] In this array five integers can be stored.
Character array-char x[5] In this array five charactgers or a word of five characters can be stored. It can be accessed using %s.
Float array-float x[5] In this array five float i.e decimal values can be stored.
Representation-x[1 2 3 4 5]
The numbers in are examples.
Double x[5] In this array five float can be stored.
Eg.
Void main()
{
Int x[5],y;
Printf(“Enter five numbers”);
For(y=0;y<5;y++)
{
Scanf(“%d”,&x[y]);
}
getch();
}
In this example array of five integers is declared. Firstly the printf statement will be executed. Then for loop executes. The concept of loop will be discussed later. Assume that for loop is used to accept values from user. By using the loop the five values entered by user are stored in the array which can be used. Similarly the array of Float and characters can be used.

No comments:

Watch tv!