Pages

Saturday 19 March 2011

PROGRAM TO CALCULATE AVERAGE OF 'n' ELEMENTS USING ARRAY

DESCRIPTION-: This program calculate the average of all the elements of an input array.this program firstly calculate the sum of the elements of array and after calculating the sum it calculate the average of elements of array by dividing sum to the number of elements in an array.
FOR EXAMPLE-: If we have elements of array as-------
                      2    4    22    54    8    1

Then it generates output as------
               average=15.16
        
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,sum=0,a[20];
float avg;
cout<<"enter number of elements in array";
cin>>n;
cout<<"enter value of elements of array"<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"sum of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
cout<<"sum ="<<sum<<"\n";
avg=sum/n;
cout<<"average ="<<avg;
getch();
}

0 comments:

Post a Comment

 
Copyright (c) 2010 Concepts Of C++. Design by WPThemes Expert

Blogger Templates and RegistryBooster.