IN C++ PLEASE
Topics
Loops
While statement
Description
Write a program that will compute the sum of one or more data values entered by the user. First,
the program will ask the user to provide the data count. Then it will ask the user data count times
to enter a data value. It will then display the sum of data values entered by the user. See the Test
section below for test data.
Requirements
Do this assignment using a While statement.
Test
For turning in the assignment, perform the following test run using the input shown.
(The user input is shown in bold)
Test Run
Enter data count
5
Enter a data value:
10
Enter a data value
50
Enter a data value
40
Enter a data value
20
Enter a data value
30
Sum: 150
(Above, user input is shown in bold)
Submit
Copy the following in a file and submit the file:
Output of the test run
C/C++ Source code
Sample Code
//variable declarations
//The variable below contains the running sum.
//It contains the sum of all the values entered by the user so far.
double sum;
//The variable below contains user input.
//It contains the most recent value entered by the user
double num;
//The variable below contains the data count.
//It contains the number of values the user is going to enter.
//It contains the number of times the While loop should loop (go around).
int dataCount;
//The variable below contains the loop count.
//It contains the number of times the While loop have looped (gone around) so far.
int count;
//input data count
cout << "Enter data count" << endl;
cin >> dataCount;
//initialize sum
sum = 0;
//Initial Setup
count = 0;
//Test
while (count < dataCount)
{
//input a number
cout << "Enter a data value" << endl;
cin >> num;
//add num to sum (add a code line below)
//Update Setup
count = count + 1;
}
//display sum (add a code line below)
IN C++ PLEASE     Topics   Loops While statement   Description   Write.pdf

IN C++ PLEASE Topics Loops While statement Description Write.pdf

  • 1.
    IN C++ PLEASE Topics Loops Whilestatement Description Write a program that will compute the sum of one or more data values entered by the user. First, the program will ask the user to provide the data count. Then it will ask the user data count times to enter a data value. It will then display the sum of data values entered by the user. See the Test section below for test data. Requirements Do this assignment using a While statement. Test For turning in the assignment, perform the following test run using the input shown. (The user input is shown in bold) Test Run Enter data count 5 Enter a data value: 10 Enter a data value 50 Enter a data value 40 Enter a data value 20
  • 2.
    Enter a datavalue 30 Sum: 150 (Above, user input is shown in bold) Submit Copy the following in a file and submit the file: Output of the test run C/C++ Source code Sample Code //variable declarations //The variable below contains the running sum. //It contains the sum of all the values entered by the user so far. double sum; //The variable below contains user input. //It contains the most recent value entered by the user double num; //The variable below contains the data count. //It contains the number of values the user is going to enter. //It contains the number of times the While loop should loop (go around). int dataCount; //The variable below contains the loop count. //It contains the number of times the While loop have looped (gone around) so far. int count;
  • 3.
    //input data count cout<< "Enter data count" << endl; cin >> dataCount; //initialize sum sum = 0; //Initial Setup count = 0; //Test while (count < dataCount) { //input a number cout << "Enter a data value" << endl; cin >> num; //add num to sum (add a code line below) //Update Setup count = count + 1; } //display sum (add a code line below)