Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get sum and mean of an array

Status
Not open for further replies.

Stjuice

Programmer
Aug 22, 2001
4
US
I wrote out a program that asks the user to input the length they would like the array to be and to input integers in the array, then it out puts each number at each index. What can i do to get the program to output the sum of the array and the mean of the array??

#include<iostream.h>


void main()
{
int x[100];
int i, count, y, sum = 0;

cout<<&quot;Enter how long? &quot;;
cin>>count;

for(i = 0; i < count; i
{
cout<<&quot;Enter value? &quot;;
cin>>y;
x= y;
}

for(i = 0; i < count; i++)
{
cout<<&quot; Array X at index &quot;<<i<<&quot;= &quot;<<x<<endl ;

}
}
 
Code:
int sum = 0;

for(int i = 0;i<count;i++)
{
   cout<<&quot;array x at index &quot;<<i<<&quot; = &quot;<<x[i]<<endl;
   sum+=x[i];
}
cout<<&quot;Sum = &quot;<<sum<<endl;
cout<<&quot;Mean = &quot;<<(double)sum/(double)count<<endl;


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top