Using do while loop to get the average of ten numbers.

I have an assignment to do in C++. I need to use do while loop to get the average of ten numbers. I'm having a very hard time. Can someone help me out? Thanks in advance!


Last edited on
show us what you have so far.
For now I only know how to use for loop. I need some help converting this to do while loop.

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

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

  cout<<"Input the amount of numbers: ";
  cin >> num; 

    for(int i = 1; i <= num; i++){
      cout<<"Enter a number: ";
	  cin >> input;
      sum += input;
    }

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

return 0;
}
It's trivial to convert a for loop to a while loop.

The for loop statement already contains:

- initialisation of pre-loop stuff
- the condition that will be checked on every iteration to see if the loop keeps gpoing
- the thing that changes in every iteration of the loop

That's all you need to make a while loop. If you understand the syntax of a for loop, and the syntax of a while loop, then you have everything you need.

(And if you don't understand the syntax of those, you should really be going and learning that, before attempting to write code.)
Topic archived. No new replies allowed.