ARRAY
An array is a collection of similar type of data item and
each data item is called an element of the array.
Declaration of
Array:
Like other simple variable, array should be declared
before they are used in the program. The syntax for declaration of an array is
:
Data_type array_name[size];
Like int age[90];
Float salary [13];
Char grade[20];
Accessing 1-D
array element
The element of an array can be accessed by specifying the
array name followed by subscript in brackets.
Let us take an array
: int arr[5];
The element of this array are: arr[0],arr[1],arr[2],arr[3],arr[4].
Here 0 is lower bound and 4 is upper bound.
e.g:
#include<stdio.h>
int main()
{
int a[5];
printf(“size of
array:%d\n”,sizeof(a));
return 0;
}
OUTPUT:20
Note-worthy point regarding array:
·
If size of array is n l.e a[n] and we supply
less than size of array then remaining data would be zero.
·
If array is initialized then to give size of
array is optional.
Processing 1-D
arrays:
1.
Reading values in arr
For(i=0;i<10;i++)
Scanf(“%d”,&arr[i]);
2.
Displaying value of arr:
for(i=0;i<10;i++)
printf(“%d”,arr[i]);
Lets see an example on this:
#include<stdio.h>
Int main(void)
{
Int arr[5],I;
For(i=0;i<5;i++)
{
Printf(“enter
the number:”);
Scanf(“%d”,&arr[i]);
}
Printf(“You have
entered:”);
For(i=0;i<5;i++)
{
Printf(“%d\t”,arr[i]);
}
}
Two dimentional array:
The syntax of 2-D array is :
Data_type array_name[row_size][column_size]
Here row_size specifies the number of row where as column_size
specifies the number of column.
Lets see the example:
Add two matrix of order 3*3
#include<stdio.h>
Int main()
{
Int a[3][3], I,j,b[3][3],c[3][3];
For(i=0;i<3;i++)
{
For(j=0;j<3;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
Printf(“enter second matrix\n:”);
For(i=0;i<3;i++)
{
For(j=0;j<3;j++)
{
Scanf(“%d”,&b[i][j]);
}
}
///addition of two entered matrix is happening here
For(i=0;i<3;i++)
{
For(j=0;j<3;j++)
{
C[i][j]=a[i][j]+b[i][j];
}
}
///displaying…
For(i=0;i<3;i++)
{
For(j=0;j<3;j++)
{
Printf(“%d\t”,c[i][j]);
}
Printf(“\n”);
}
}//main close here
No comments:
Post a Comment