Find average of array in C++ by R4R Team

Example-
Array is: [1,2,3,4,5]
Average is: 3

program:

#include < iostream>
using namespace std;
class r4r
{
public:
int average(int *a, int n)
{
int i,sum=0,avg;
for(i=0;i< n;i++){
sum=sum+a[i];
}
avg=sum/n;
cout<<"Average is: "<< avg;
}
};
int main()
{
int arr[10],i;
cout<<"Enter 5 element in array"<< endl;
for(i=0;i< 5;i++){
cin>>arr[i];
}
//create class object
r4r obj;
obj.average(arr,5);
return 0;
}


output-

Enter 5 element in array
1 2 3 4 5
Average is: 3

-In this program, firstly we take an input in array then for finding the average, we find the sum of all elements and divide by the total number of element.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!