Question

In: Computer Science

Write a program that uses a while loop with a priming read to ask the user...

Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop.

Here is what a sample run should look like:

Enter the first number (-1 to quit):<SPC>20<ENTER>
Enter another number (-1 to quit):<SPC>92<ENTER>
Enter another number (-1 to quit):<SPC>34<ENTER>
Enter another number (-1 to quit):<SPC>76<ENTER>
Enter another number (-1 to quit):<SPC>97<ENTER>
Enter another number (-1 to quit):<SPC>57<ENTER>
Enter another number (-1 to quit):<SPC>-1<ENTER>
The total is 376, the average is 53.71<LINERETURN>

Solutions

Expert Solution

Solution

Code

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int number,count=0;
float total=0.0,average;
cout<<"Enter the first number (-1 to quit): ";
cin>>number;
while(number!=-1)
{
total=total+number;

cout<<"Enter the another number (-1 to quit): ";
cin>>number;
count++;
}
average=total/(count+1);
cout<<"The total is "<<total<<", the average is "<<fixed<<setprecision(2)<<average;

return 0;
}
Screenshot

Output

--

programming language not mentioned

so i have done using c++

if you have any doubt, please mention it

love to help

all the best


Related Solutions

Write a program that uses a while statement to read integers from the user and prints...
Write a program that uses a while statement to read integers from the user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters 999. The average should be output with 4 digits of precision. c++ please ASAP
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
Write a while loop to ask the user to type number of hours(double) they work per...
Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT