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
  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • What did I do wrong with this C++ code? Assume that we don't need to ask...

    What did I do wrong with this C++ code? Assume that we don't need to ask users for the number of students. #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size; int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total; // average grade and total grades //**************start your code here************************ cout<<"How many student grades...

  • #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers)...

    #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • I am working on this switch program everything seems to be ok but the compiler is...

    I am working on this switch program everything seems to be ok but the compiler is giving me an error on the final closing brace and wanted to know where I am making an error #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int choice; char repeat; double MidTerm = 0; double FinalExam = 0; double Quiz1 = 0; double Quiz2 = 0; double MidTermGrade = 1, FinalExamGrade = 2, Quiz1Grade = 3, Quiz2Grade = 4,...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

  • Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how

    This is what I have as of right now. I am lost on what to do next#include <stdio.h>#include <iostream>using namespace std;int main(){    double scores, numOfStudents, average = 0, highscore, sum = 0;    string names;    cout << "Hello, please enter the number of students" << endl;    cin >> numOfStudents;    do {        cout << "Please enter the student's name:\n";        cin >> names;        cout << "Please enter the student's...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

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