Question

Question 2: This program continue asking for a new number until the user enters a 0...

Question 2:

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

#include <iostream.h>
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 the sum to 0 to make sure the 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;
        }

system("pause");

        return 0;
}


In the program,right at the beginning you have initialized the choice to 1. You did that to get 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 above program so that is uses do ... while. Do not initialize the choice to 1 this time and compile and run the program. Does the program work the same way?

Thanks so much! If any clarification is needed, just comment.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Modified Program

#include <iostream>

using namespace std;

int main()
{
int x;
int count = 0; // (1) initialize a counter to 0 to count number of values
///********* choice IS NOT INITIALIZED HERE ******************/////
int choice; // This is the choice that controls the looping continuation or termination
double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0
double average;

/***** TO MAKE THE LOOP EXECUTE ATLEAST ONCE WITHOUT CHECKING FOR ANY
* CONDITION, WHILE LOOP IS MODIFIED WITH DO..WHILE LOOP *///
do // (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;
}while( choice == 1);

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;
}

system("pause");

return 0;
}

===============================================================================

#include <iostream> using namespace std; int main() { int x; **///// int count = 0; // (1) initialize a counter to o to countif(count 0) cout << You havent entered any number, no average will be computed, bye \n; else{ average = sum/count; //Notic

Enter a grade <Enter>: 60 Do you wish to enter another grade? (1 for yes and 0 or other key for no): 1 Enter a grade <Enter>:

Add a comment
Know the answer?
Add Answer to:
Question 2: This program continue asking for a new number until the user enters a 0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT