Traversal


Traversing the array element means accessing each and every element of the array for specific purpose.If A is an array of homogeneous data element.then traversing the data element can include printing every element,counting the total number of elements,or performing any processing on these element.Traversal in one dimensional array.
The algorithm for array traversal is:Step 1  initialize index to the lower bound of the array.in step 2 a while loop is executed. step 3 processes the individual array element as specific by the array name index value.step 4 increments the index value so that the next array could be processed.


Write a program to find mean of n numbers using arrays.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,arr[20];sum=0;
float mean=0.0;
clrscr();
printf("\n Enter the number of elements in the array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n arr[%d]=",i);
scang("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum=arr[i];
mean=sum/n;
printf("\n The sum of the array elements=%d",sum);
printf("\n The mean of the array elements=%f",mean);
getch();
}


Output

Enter the number of elements in the array:5
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5
The sum of the array elements=15
The mean of the array elements=3.00


Comments