C Programming Tutorial
Computer Tutorial

Array



Array

An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Thus, an array is a collection of similar elements. These similar elements could be all int's, or all float's, or all char's, etc. Usually, the array of characters is called a ‘string’, whereas an array of int's or float's is called simply an array. Remember that all elements of any given array must be of the same type.
The general form of array is :
variable type         name of array [size of array];
Ex:
int num[5];
float dec[20];
char name[25];

Types of Array

1. Single-Dimension Arrays (1-D Array)
2. Two-Dimensional Arrays (2-D Array)
3. Multidimensional Array

A Simple Program Using Array to find the sum and average of five subject marks.

\* A Simple Program Using Array to find the sum and average of five subject marks *\
# include < stdio.h >
int   main( )
{

int   i, avg, sum = 0  ;
int   marks[5]  ;/* array declaration */
for ( i = 0; i < 5; i ++  )
{

printf("\n Enter marks ") ;
scanf(" %d ",   &marks[i] ) ;/* store data in array */

}
for ( i = 0; i < 5; i ++  )
{

sum = sum + marks[i] ;/* read data from an array */
avg = sum / 5 ;
printf("\n Average marks ") ;
printf(" %d ",   avg ) ;

}
return ( 0 );

}