New program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compsci
    New Member
    • Jan 2007
    • 32

    New program

    I'm trying to write a program that does the following:


    1. Repeatedly read a student score (for an exam) and add it to a running sum. Count the number of scores entered. (We’ll assume that the scores are between 0 and 100.) Stop when the user enters a negative number. Output the sum and count of the scores.
    2. Define a function average that, passed the sum of the scores and the count of the scores, computes the average score, which it returns. In main, call this function just after the above loop, and output the average score that the function returns.
    3. Define a function validScore that, passed a score, returns true if the score is between 0 and 100 and otherwise returns false. Using this function, modify the loop that reads in the scores so that, if the score is not valid, it is echoed with a message saying that the score is invalid, and so that the score is counted and added to the sum only when it is valid. (Note that there is some redundancy as validScore is used here since the loop exits when a negative score is entered—so validScore is never passed a score that is invalid because it is negative.)
    4. Define a function letterGrade that, passed a valid score (one between 0 and 100), returns a letter grade according to the following schedule:
    ‘A’: 90 and above
    ‘B’: 80-89
    ‘C’: 70-79
    ‘D’: 60-69
    ‘F’: below 60
    Use this function to find the letter grade corresponding to each valid score and output the letter grade as soon as it is found. Keep a count of the number of each letter grade (‘A’-‘F’) and output each count at the end of the run.

    I have some of it right here. Can anyone help me?

    Code:
    include<iostream>
    using namespace std;
    int main() {
     int countScores = 0;
     int sumScores = 0; 
     cout <<"Enter score"<< endl;
     int n = 0;
     cin>>n;
     while(n >= 0) {
      sumScores = sumScores + n;
      countScores++;
      cin>>n;
     }
     cout<<"You entered "<<countScores<<" numbers"<<endl;
     cout<<"Their sum is :"<<sumScores<<endl;
     return 0;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by compsci
    I'm trying to write a program that does the following:


    1. Repeatedly read a student score (for an exam) and add it to a running sum. Count the number of scores entered. (We’ll assume that the scores are between 0 and 100.) Stop when the user enters a negative number. Output the sum and count of the scores.
    2. Define a function average that, passed the sum of the scores and the count of the scores, computes the average score, which it returns. In main, call this function just after the above loop, and output the average score that the function returns.
    you have part 1 working so you can now do part 2

    Comment

    • compsci
      New Member
      • Jan 2007
      • 32

      #3
      Originally posted by horace1
      you have part 1 working so you can now do part 2
      That is what I need help on.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by compsci
        That is what I need help on.
        can you work out the average in main() then when that works figure out how to use a function
        Code:
         cout<<"You entered "<<countScores<<" numbers"<<endl;
         cout<<"Their sum is :"<<sumScores<<endl;
        int average= ...                ;
        cout << "average is :" << average<<endl;

        Comment

        • compsci
          New Member
          • Jan 2007
          • 32

          #5
          [QUOTE=horace1]can you work out the average in main() then when that works figure out how to use a function
          [code]
          Code:
          cout<<"You entered "<<countScores<<" numbers"<<endl;
           cout<<"Their sum is :"<<sumScores<<endl;
          int average= ...                ;
          cout << "average is :" << average<<endl;
          I think I would have to divide countScores by sumScores.

          Code:
          cout<<"You entered "<<countScores<<" numbers"<<endl;
           cout<<"Their sum is :"<<sumScores<<endl;
          int average= countScores / sumScores;
          cout << "average is :" << average<<endl;
          Do I have it?

          Comment

          • compsci
            New Member
            • Jan 2007
            • 32

            #6
            I'm not too sure aboit the function I made. Is it right?


            Code:
            include<iostream>
            using namespace std;
            
            funct average(countScores, sumScores);
            average = countScores / sumScores;
            return average;
            
            int main() {
             int countScores = 0;
             int sumScores = 0; 
             cout <<"Enter score"<< endl;
             int n = 0;
             cin>>n;
             while(n >= 0) {
              sumScores = sumScores + n;
              countScores++;
              cin>>n;
             }
             cout<<"You entered "<<countScores<<" numbers"<<endl;
             cout<<"Their sum is :"<<sumScores<<endl;
             cout<<"average"<<average<<endl;
            
             return 0;
            }

            Comment

            • beck322
              New Member
              • Jan 2007
              • 12

              #7
              Originally posted by compsci
              I'm not too sure aboit the function I made. Is it right?


              Code:
              include<iostream>
              using namespace std;
              
              funct average(countScores, sumScores);
              average = countScores / sumScores;
              return average;
              
              int main() {
               int countScores = 0;
               int sumScores = 0; 
               cout <<"Enter score"<< endl;
               int n = 0;
               cin>>n;
               while(n >= 0) {
                sumScores = sumScores + n;
                countScores++;
                cin>>n;
               }
               cout<<"You entered "<<countScores<<" numbers"<<endl;
               cout<<"Their sum is :"<<sumScores<<endl;
               cout<<"average"<<average<<endl;
              
               return 0;
              }

              Maybe this can help u??????

              #include <iostream>
              #include <iomanip>

              using namespace std;


              int main()
              {
              int quiz1, // Quiz 1 grade
              quiz2, // Quiz 2 grade
              quiz3; // Quiz 3 grade

              double average; // Average of the three quiz grades

              // Setup output stream for one decimal place
              cout << setprecision(1)
              << setiosflags(ios ::fixed)
              << setiosflags(ios ::showpoint) ;

              cout << "Enter the grade for Quiz 1: " ;
              cin >> quiz1 ;
              cout << "Enter the grade for Quiz 2: " ;
              cin >> quiz2 ;
              cout << "Enter the grade for Quiz 3: " ;
              cin >> quiz3 ;

              average = double (quiz1 + quiz2 + quiz3) / 3;

              cout << endl;
              cout << " The average of the three quizzes is " << average << endl;

              return 0;
              }

              Comment

              Working...