In: Computer Science
what is the calculated the average code using C++
Having a hard time trying to get the float average.
CODE:
#include<iostream>
using namespace std;
int main()
{
//array of float values for which we need to calculate the
average.
float array1[6]={10.0,20.5,13.7,14.3,19.7,25.3};
int i;//loop variable.
float sum=0.0;//variable to the sum of the elements.
cout<<"The array elements are : ";
//loop to calculate the sum of the values of the array.
for(i=0;i<6;i++)
{
cout<<array1[i]<<" ";
sum=sum+array1[i];
}
float average;//a float variable to store the float average.
//we know average is sum of elements divided by number of
elements.
//here the sum of elements is stored in sum variable.
//and we know there are 6 elements.
average=sum/6;//calculating average.
//printing the average.
cout<<endl<<"The float average of elements is
"<<average<<endl;
}
CODE ATTACHMENTS:
OUTPUT:
I have took a float elements array.Then calculated the sum using a loop.
Then calculated the average of the elements.
Please do comment for any queries.
PLease like it.
Thank You.