Question

In: Computer Science

Exercise 3.4.1 In P3_4_1.cpp (BELOW), right at the beginning, the program initializes choice to 1. This...

Exercise 3.4.1

In P3_4_1.cpp (BELOW), right at the beginning, the program initializes choice to 1. This forces the while loop to run at least once. If you use a do ... while instead, you wouldn't need to initialize the choice. Re-write the below program, call the new program ex341.cpp, so that is uses do ... while. Do not initialize the choice to 1 this time. Compile and run the program.

Answer questions:

Does the program work the same way? Write your answer as a comment inside the code of your ex341.cpp file.  

// P3_4_1.cpp - Read and average some integers, print the result.

// This program continue asking for a new number until the user enters a 0 to terminate the program

#include <iostream>  using namespace std;  

int main(void)  

{      int x;

        int count = 0; // (1) initialize a counter to 0 to count number of values  

        int choice = 1; // This is the choice that controls the looping continuation or termination          

        double sum = 0; // initialize sum to 0 to make sure sum at the beginning is 0              

        double average;  

        while( choice == 1) // (2) read N grades and compute their sum, count ensures N entries  

       {

              // read each number and compute the sum:  

              cout << "\n Enter a grade <Enter>: ";               

              cin >> x;               

              sum = sum + x;  

             count++; // (3) update the count  

            // prompt the user:              

            cout << "Do you wish to enter another grade? (1 for yes and 0 or other key for no): "

                   << endl;  

            cin >> choice;

        }

        if(count == 0)  

              cout << "You haven't entered any number. No average will be computed. Bye!\n";            

        else{

              average = sum/count; //Notice that we have divided by count this time  

              cout << "The average of these " << count << " grades is " << average <<"." << endl;  

        }

        return 0;  

}  

Solutions

Expert Solution

Here is code:

#include <iostream>
using namespace std;
int main(void)
{
int x;
int count = 0;
int choice;
int N; // Number of values for which the average must be computed.
double sum = 0;
double average;
char repeat;
do
{
count = 0; // (1) initialize a counter to 0 to count number of values
choice = 1; // This is the choice that controls the looping continuation or termination
while (choice == 1) // (2) read N grades and compute their sum, count ensures N entries
{
// read each number and compute the sum:
cout << "\n Enter a grade <Enter>: ";
cin >> x;
sum = sum + x;
count++; // (3) update the count
// prompt the user:
cout << "Do you wish to enter another grade? (1 for yes and 0 or other key for no): "
<< endl;
cin >> choice;
}
if (count == 0)
cout << "You haven't entered any number. No average will be computed. Bye!\n";
else
{
average = sum / count; //Notice that we have divided by count this time
cout << "The average of these " << count << " grades is " << average << "." << endl;
}
// (4) read user input and wheather user wants to play again.
cout << "\nDo you want to run again (Y/N) : ";
cin >> repeat;
} while (repeat == 'Y');
return 0;
}

Output:


Related Solutions

Exercise 5.5 PART A Copy or cut and paste program P5_2.cpp (located below) to a new...
Exercise 5.5 PART A Copy or cut and paste program P5_2.cpp (located below) to a new program called ex55.cpp. Make PI a constant value. Compile and run the program for the following values: r = 2 cm, h = 10 cm The correct answer should be:       The cross-section area of the cylinder is 3.89556 inch-sq       The side area of the cylinder is 19.4778 inch-sqr Did you get the correct answer? You didn't! Explain the reason for this logic...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the...
First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the program and make sure it produces the correct results. Here is what you need to do for the exercise:     Overload the % operator such that every time you use it, it takes two objects of type AltMoney as its arguments and returns:             a) 5% of the difference between the income and expenditure, if income is larger than the expenditure             b) -2%...
1. Some bene?ts of the OAS program are means-tested, while the CPP program is universal. Discuss...
1. Some bene?ts of the OAS program are means-tested, while the CPP program is universal. Discuss two reasons why a universal program might be preferable over a means-tested program.
Match the right choice with the right question by placing the right letter in the far...
Match the right choice with the right question by placing the right letter in the far left column. The letters go with the questions. Mark Choices Letter Questions Boiling Point A Moves heat from cold reservoir to a hot reservoir Convection B In this process the volume remain constant. Radiation C The sum of the Kinetic and Potential energies of the particles in a system. Kelvin D The amount of heat necessary to change a liquid to a gas at...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT