Question
Topics C++ only! Loops while statement Description Write a program that will compute the average of a set of decimal numbers provided by the user.
Topics
C++ only!
Loops
while statement
Description
Write a program that will compute the average of a set of decimal numbers provided by the user. The program will ask the user to enter the numbers one at a time. The user will indicate the end of data by entering q when asked for a number. The user will enter numbers and the program will keep a count of the numbers entered. At the end, the program will display the sum, the count and the average of the numbers entered. See thr Test section below for test data.
Requirements
Do this assignment, using a While statement.
Testing
For doing the assignment, perform the following test run using the input shown.
Test Run
Enter a number:
10.2
Enter a number:
50.4
Enter a number:
40.6
Enter a number:
20.8
Enter a number:
30.0
Enter a number:
q
Sum: 152.0
Count: 5
Average: 30.4
Submit
Copy the following in a file and submit the file:
Output of the test run
C/C++ Source code
Signaling End of Data
We often input data values using a While loop and use different schemes for ending the loop.
Input Data Size
In this scheme, you ask the user for data count before inputting the data values. Then, you can setup the While statement to loop data count times. In the code below, first we input the data count. Then we setup the While statement to loop data count times. In each pass through the loop, we input a data value and process it.
int count, dataCount;
double num;
cout << Enter data count<< endl;
cin > dataCount;
count=0;
while (count < dataCount)
{
cout << Enter a number<< endl;
cin > num;
//Process data
}
Using a Sentinal
The users do not like the scheme of asking the user for data count up front. They prefer to simply enter the data and signal when it ends and let the program figure out its size. In that case, you need a mechanism for indicating end of data.
One method is to think of a valid input value that is never going to be part of data. For example, if the data is never going to have negative values. Then you can use a negative value such as -1 to indicate end of data. This type of end of data value is called sentinel.
In this scheme, you do your first input just before the While statement (Initialize). Then you test in the While statement that input is a good data value (input >= 0). If so, enter the loop and process the data value. You do the second input and all further inputs as the last statement in the loop (Update). This setup ensures that your input is always immediately followed by the Whiles statement checking whether input is good data value. If so, enter the loop and process the data value. If it is sentinel (negative value). Note that this setup follows the rhythm in which Input is immediately followed by Test
Input Test Process Input Test Process..Input Test (end loop)
//Input
cout << Enter a number
cin > num;
//Test
while (num >= 0)
{
//Process
//Input
cout << Enter a number
cin > num;
}
Checking Cin Value
In cases, where we can't find a workable sentinel, we use this scheme as the last resort. In this scheme, we rely on detecting the failure of input as the end of data.
When you issue a cin for input, it always has a target variable where the input value is stored. From knowing the type of this input variable, cin knows what type of value to expect the user to enter. For example, if the type of the target input variable is double, cin expects the user to enter a decimal number. If the user enters q, cin will consider it an invalid input. Cin would fail and will not store q in the target input variable because the type of input variable is different from the type of data that user entered. Instead, the input will stay in the input stream and further inputs will not occur.
In this scheme, we use the failing of input process as the signal for end of data. In the sample code below, the target of cin is num which is a double variable. So, cin expect the user to enter a decimal value. If the user enters q, cin would fail, would no longer work and would no longer do any further inputs. We can detect that condition by checking if cin failed. If it failed, we would conclude that the user is finished entering the data.
This code is similar to the code for sentinel. In the sentinel scheme, we test for invalid data value. In this scheme, we test for invalid input value and for input to fail.
cout << Enter a number
cin > num;
while (!cin.fail())
{
cout << Enter a number
cin > num;
}
Sample Code
/*
Note that in sample code below, for inputting, we issue cin << num where num is double. So, cin expects the user to enter a decimal number (or a whole number that cin will convert to decimal). But, if the user enters q, cin will consider it an invalid input and cin will fail. We can detect that and would know that user has entered q and will regard it as the end of data.
*/
//declare variables
//num will keep the number entered by the user
//sum will keep the sum of the numbers entered by the user
//count will keep the count of the numbers entered by user
//average will be used to store the average of all the numbers
double num;
double sum=0;
double average;
int count=0;
//Initial setup
//Input a number
cout << Enter a number << endl;
cin > num;
//Test
while (!cin.fail())
{
//Write code that processes the number entered.
//Process the number by adding num to sum
// add 1 to count
count = count + 1;
//Update setup
//Input a number
cout << Enter a number << endl;
cin > num;
}
//calculate average by dividing sum with count
//display sum, count and average
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started