### For loop

Ok so I need to make two for loops. I first one will ask for 10 numbers and then it will print the largest number out of the 10 numbers entered. The second one will also ask for 10 numbers and then calculate the average of all the numbers.

Here is what I have so far for the first code:

 12345678 int a[10], large; cout << "Enter 10 numbers" << endl; for(int i=0;i<10;i++) cin>>a[i]; for(int i=0;i<10;i++) { }

And I am kind of stumped on what to do for the average.. thanks for any help
A tip, please do figure the missing lines yourself ;)
 1234567891011 int sumOfAllElements = 0; for(int i =0; i < 10; i++) { cout << " Element #" << i << " is "<< a[i]; // how to get the sumOfAllElements? // ... } int average; // what to do with sumOfAllElements to get average? // ...
Last edited on
I do understand how to get to where you are at in the code. But i'm not sure how to make a for loop for this to figure out the largest number out of 10 numbers and average of the 10 numbers (remember this will be two separate programs)
You just keep a running total of entered numbers in an integer. To get an average, you would just then divide by the number of enter numbers, which in this case would be 10.
the largest
 123456789 int a[10], large=0;//<<<>a[i]; for(int i=0;i<10;i++) { if(a[i]>large) large=a[i]; }
How would I get the average from here? I'm confused as to how I would keep a running total..

 1234567891011 int a[10], average=0; cout << "Enter 10 numbers" << endl; for(int i=0;i<10;i++) cin>>a[i]; for(int i=0;i<10;i++) { a[i]=a[i]+a[i] average = a[i]/10 } cout << average << endl;
hah no dude :D
int total=0;

for():::
total=total+a[i]

:::

cout<<total/10
by division by 10 u ll get the average
something like that :PP
Last edited on
what Mats said but store it in a float
Topic archived. No new replies allowed.