Jan 15, 2017

Arrays

Arrays
An array is a group of related data items that share a common name. For instance, an array name salary to represent a set of salaries of a group of employees.
For example, salary[10]
Represents the salary of the 10th employee.
One dimensional Array
A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or one –dimensional array.In C, single-subscripted variable  can be expressed as x[1], x[2], x[3],….. x[n]. The subscript can begin with number 0. That is x[0] is allowed. For example, if we want to represent  a set of five numbers, say(35,40,20,57), by an array variable number, then the declaration will be
int number [4].
The values can be assigned and stored as follows:
35
40
20
57
number[0]=35;
number[1]=40;
number[2]=20;
number[3]=57;


Following are valid statements:
a=number[0]+10;
number[4]=number[0]+number[2];
number[2]=x[5]+y[10];
value[6]=number[i]+3;
The subscript of an array can be integer constants, integer variables like i, or expressions that yield integer.

Array elements are like normal variables
c[ 0 ] =  3;
printf( "%d", c[ 0 ] );
Perform operations in subscript.  If  x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]

The subscript of an array can be integer constants, integer variables like i, or expressions that yield integer.
Declaration of arrays
General form of array  declaration is
type arrayname[ size ];
First element at position 0,n element array named c:
c[ 0 ], c[ 1 ]...c[ n – 1 ]
Declaring multiple arrays of same type. Format similar to regular variables
Example:
int b[ 100 ], x[ 27 ];

Initialization of one dimensional Arrays
type array-name [size]={list of values};
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
All elements 0
If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array

Example of one dimensional array
#include <stdio.h>
int main()
{
int a[3], i;;

printf("\n\t Enter three numbers : ");
for(i=0; i<3; i++)
{
scanf("%d", &a[i]);  // read array
}
printf("\n\n\t Numbers are : ");
for(i=0; i<3; i++)
{
printf("\t %d", a[i]);  // print array
}

}
Array and strings
Character arrays:
String “first” is really a static array of characters
Character arrays can be initialized using string literals
char string1[] = "first";
Null character '\0' terminates strings
string1 actually has 6 elements
It is equivalent to
char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };
we can access individual characters
string1[ 3 ] is character ‘s’
Array name is address of array, so & not needed for scanf
scanf( "%s", string2 );

Two dimensional/ multidimensional arrays
Multiple subscripted arrays
Tables with rows and columns (m by n array)
Like matrices: specify row, then column


Two dimensional arrays are declared as follows:
type array-name[row size][coloumn-size];

The general form of multidimensional array is:
type array-name[s1][s2][s3]… [sm];
Two dimensional arrays may be initialized as follows
int  table[2][3]={0,0,0,1,1,1}; This statement can be equivalently written as
0 0 0
1 1 1
int table[2][3]={{0,0,0},{1,1,1}};


If not enough, unspecified elements set to zero
0 1
3 4
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
Referencing elements
Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );

Example of two dimensional array
#include <stdio.h>
int main()
{
int a[2][2];
int b[2][2];


printf("\n\t Enter three numbers : ");
for( int i=0; i<2; i++)
{
for( int j=0; j<2;j++)
scanf("%d", &a[i][j]);  // read array
}

printf("\n\n\t Numbers are : ");
for(int k=0; k<2; k++)
{
for(int l=0;l<2;l++)
printf("\t %d", a[k][l]);  // print array
printf("\n\n\t\t");
}

}






No comments:

Post a Comment