Question: IN C++ PLEASE Topics Loops while statement Description Write a program that will compute the average of a set of decimal numbers provided by the

IN C++ PLEASE

Topics

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

Often, we input data values using a While loop and use different schemes for ending the loop. Three commonly used schemes are described below.

Input Data Size

In this scheme, we ask the user for data count before inputting the data values. Then, we setup the While statement for looping data count times.

In the code fragment 1 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.

//Code Fragment 1

int count, dataCount;

double num;

cout << Enter data count<< endl;

cin > dataCount;

count=0; //initialization

while (count < dataCount)

{

cout << Enter a number<< endl;

cin > num;

//Process data

count++; //update

}

Using a Sentinel

In In this scheme, we don't ask for the data count up front. Instead, we ask the user to end the data with a designated data value not used in the data. This value is called the sentinel.

For example, if the data consists of zeros and positive whole numbers such as test scores, we can designate -1 as the sentinel or end of data value. In that case, when the user is asked to enter a data value and the user has already entered all the data values, the user enters -1. Each time, the program inputs a data value, it checks whether it is -1, if so, it ends doing the input. The data type of the sentinel should be the same as other data values so that the program inputs it like other data value. For example, if the data value (e.g. test scores) are of type int, the sentinel should also be of type int. If the sentinel is of some other type such as double or string, it will not get inputted as a data value and the program will not be able to check it.

In the code fragment 2 below, we implement this scheme. Normal data values are zeros and positive whole numbers. The sentinel is -1. The data values and the sentinel are of type int.

In the code below, we input just before the While statement (Initialize). Then we test in the While statement that input is a good data value (input >= 0). If so, we enter the loop and process the data value. We do the second input and all further inputs as the last statement in the loop (Update). After that the loop re-executes the While statement which checks whether input is a good data value. If so, we enter the loop and process the data value. If it is sentinel (negative value), we exit the loop.

//Code Fragment 2

int num;

//Input

cout << Enter a number << endl; //initialize

cin > num; //initialize

//Test

while (num >= 0) //Test

{

//Process

//Input

cout << Enter a number<< endl; //update

cin > num; //update

}

Checking Cin status

In cases, when we cannot find a workable sentinel, we use this scheme. In this scheme, we use a Not Good input status (input failing) as the end of data.

When we input using a cin, we specify a target input variable as part of the cin instruction. We declare the data type of the input variable before issuing cin. So, cin knows the data type of the target input variable. From knowing the data type of the input variable, cin knows what type of values to expect from the user. For example, if the data type of the input variable is double, then cin expects the user to enter a decimal number. If the user enters q, cin will considers it an invalid input. It will not store q in the target input variable because the data type of the input variable is different from the data type of q. Instead, cin will record the status of the input to be Not Good (failed). After issuing each input (cin), we can check the status of the input (cin) whether it is Good or Not Good (failed). If the input status is Good, the input happened normally, we enter the loop and process the input. Otherwise, we end the loop.

In the code fragment 3 below, the input variable num is of type double and all decimal and whole number values are valid data values. So, we use q as end of data. We input just before the While statement (Initialize). Then we test in the While statement whether input status is good (cin.good ( )). If cin.good( ) evaluates to true, that means the input status is Good and the input happened normally. If so, we enter the loop and process the data value. We do the second input and all further inputs as the last statement in the loop (Update). After that the loop re-executes the While statement which checks whether input status is good (cin.good ( )). If cin.good( ) evaluates to true, that means the input status is Good and the input happened normallly. If so, we enter the loop and process the data value. Otherwise, the user entered q and the input did not happen. So, the While statement ends and the control goes to the instruction past the While statement.

In this scheme, we use the input status becoming Not Good (failing) as the signal for end of data.

//Code Fragment 3

double num;

//input

cout << "Enter a number" << endl; //initialize

cin > num;

//test //initialize

while (cin.good()) //test

{

// process

//input

cout << "Enter a number" << endl; //update

cin > num; //update

}

Sample Code

/*

Note that in the sample code below, we issue cin << num for inputting a value 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 considers it an invalid input and cin records the input status to be Not Good.

While statement detects the input status by issuing cin.good() in its test expression. If cin.good() returns (evaluates to) true, that mean the input did happen and the input status is Good. So, the While statement enters the loop. But if cin.good() returns (evaluates to) false, that mean the input did not happen and the input status is Not Good. This happens when the user enters 'q' to indicate end of data. The While statement does not enter the loop. It ends and the control transfers to the instruction past the While statement.

*/

//declare variables

//num is used to keep the number entered by the user

//sum is used to keep the sum of the numbers entered by the user

//average is used to store the average of all the numbers

//numCount is used to keep the count of the numbers entered by user

double num;

double sum=0;

double average;

int numCount=0;

//Initialization

//Input a number

cout << Enter a number << endl;

cin > num;

//Test

while (cin.good()) //equivalent short form: while (cin)

{

//Write code that processes the number entered.

//Process the number by adding num to sum

// add 1 to count

numCount = numCount + 1;

//Update

//Input a number

cout << Enter a number << endl;

cin > num;

}

//calculate average by dividing sum with numCount

//display sum, numCount and average

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!