WHILE Loops Other than the items listed above, the TA should not need to cover a
ID: 3671954 • Letter: W
Question
WHILE Loops
Other than the items listed above, the TA should not need to cover any additional programming concepts.
For this task, you will calculate the average of a set of numbers as provided by the user. Continue to prompt the user until the user enters a negative number (do not count this number in the average). Here is an example (bold underline indicates user input)
Enter number 1: 3
Enter number 2: 7
Enter number 3: 4
Enter number 4: 1
Enter number 5: -1
The average of the above 4 numbers is: 3.75
Explanation / Answer
#include using namespace std; int main() { int sum = 0, countnumbers = 0, int inputnum; // This is to initialize all variables while (true) { // loop forever (until break is reached below) cout inputnum; // to get user input if (inputnum < 0) break; // if the number is negative then, exit loop sum += inputnum; // if not then=> add it to the running sum countnumbers++; // now increase the count by 1 to calculate the number of elements } if (countnumbers > 0) coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.