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!
#include <iostream>
usingnamespace 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.)