Sunday, May 19, 2013

Array

Array can be defined as a collection of variables of the same type that are referred 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 heighest address to the last element. We can say that an array is a variable that is capable of holding many values. On the other hand, an ordinary variable can hold a single value at a time.




ARRAY DECLARATION AND DEFINITION

An array needs to be declared and defineed at the beginning so that the computer will know what is the data type of array elements and how large the array size, i.e., the maximum number of elements that array can hold. The general form for declaring an array is

storage_class type var_name[size];



STORING ELEMENTS IN ARRAY

An array must be both, declared and defined before it can be used. Array declaration only reserves memory for the elements in the array. No value will be stored. The value of array elements are stored when array is defined. This is also called array initialization.



Examples


#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
   float marks[5];
   float sum=0,avg;
   for(i=0;i<=4;i++)

{
  int i;
  float marks[5];
  floatsum=0,avg;
  for(i=0;i<=4;i++)
{
  printf("\n enter marks[%d]=",i);
  scanf("%f",&marks[i]);
}
for(i=0;i<=4;i++)

{
   sum=sum+marks[i];
}

avg=sum/5;
printf("average marks=%6.2f",avg);
}
getch();
}















No comments:

Post a Comment