What is an Array?
Arrays are collection of similar items (i.e. ints, floats, chars) whose memory is allocated in a contiguous block of memory.
Pointers and arrays have a special relationship. To reference memory locations,arrays use pointers. Therefore, most of the times, array and pointer references can be used interchangeably.
--------------------------------------------------------------------------------
Declaration of arrays
Arrays must be declared before they are used like any other variable. The general form of declaration is:
type variable-name[SIZE];
The type specify the type of the elements that will be contained in the array, such as int,float or char and the size indicate the maximum number of elements that can be stored inside the array.
--------------------------------------------------------------------------------
The following example illustrate the use of array:
float height[50];
Declares the height to be an array containing the 50 real elements. Any subscripts 0 to 49 are all valid. In C the array elements index or subscript begins with the number zero. So height [0] refers to first element of the array. (For this reason, it is easier to think of it as referring to element number zero, rather than as referring to first element). The declaration int values[10]; would reserve the enough space for an array called values that could hold up to 10 integers. Refer to below given picture to conceptualize the reserved storage space.
values[0]
values[1]
values[2]
values[3]
values[4]
values[5]
values[6]
values[7]
values[8]
values[9]
--------------------------------------------------------------------------------
Initialization of arrays:
We can initialize the elements in an array in the same way as the ordinary variables when they are declared. The general form of initialization off arrays is:
type array_name[size]={list of values};
The values in the list care separated by commas, for example the statement
int number[3]={0,0,0};
The initialization of arrays in c suffers two drawbacks
There is no convenient way to initialize only selected element.
There is no shortcut method to initialize large number of element
--------------------------------------------------------------------------------
The following program to count the no of positive and negative numbers
/* Program to count the no of positive and negative numbers*/
#include< stdio.h >
void main( )
{
inta[50],n,count_neg=0,count_pos=0,I;
printf("Enter the size of the array\n");
scanf(%d,&n);
printf("Enter the elements of the array\n");
for I=0;I < n;I++)
scanf(%d,&a[I]);
for(I=0;I < n;I++)
{
if(a[I]< 0)
count_neg++;
else
count_pos++;
} printf("There are %d negative numbers in the array\n",count_neg);
printf("There are %d positive numbers in the array\n",count_pos);
}
--------------------------------------------------------------------------------
Multi dimensional Arrays:
Often there is a need to store and manipulate two dimensional data structure such as the matrices & tables. Here array has two subscripts. One subscript denotes row & the other the column. The declaration of two dimension arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20];
The following program illustrate addition two matrices & store the results in the 3rd matrix
/* example program to add two matrices & store the results in the 3rd matrix */
#include< stdio.h >
#include< conio.h >
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("enter the order of the matrix\n");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("matrix can be added\n");
printf("enter the elements of the matrix a");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(%d,&a[i][j]);
printf("enter the elements of the matrix b");
for(i=0;i < p;i++)
for(j=0;j < q;j++)
scanf(%d,&b[i][j]);
printf("the sum of the matrix a and b is");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i < m;i++)
{ for(j=0;j < n;j++)
printf(%d\t,&a[i][j]);
printf(\n);
}
}