Using a for loop to average numbers.

I need to use a for loop to do this problem.

Write a program that reads a specified number of floating point numbers from the user and prints their average. The first number input by the user will be an integer that specifies how many numbers are to be averaged. The rest of the input will be the floating point numbers to be averaged. Your output should be accurate to three decimal places. For example, if the input is:
8 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35
your output should be:
average = 8.767.

I'm not sure if I am getting something mixed up inside of the loop or somewhere else.

Thanks in advance!
What's your code so far? I'd rather I could point you in the right direction than just doing it for you.
Well, you haven't replied, but here it is anyway:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
  int num;
  float input, sum=0, avg;

  
  cin >> num; 

    for(int i = 1; i <= num; i++){
      cin >> input;
      sum += input;
    }

  avg = sum / num;
  cout << "average = " << avg << endl;

return 0;
}
Sorry about the delay response. I just got out of classes. And the program worked. Thanks so much! The problem I was having is that I was trying to use your "num" where you have "i" in the for parameter. I understand why that wasn't working now. Thanks again!
No problem, glad I could help!
Nice one! This is exactly what I need.
Topic archived. No new replies allowed.