C++ question- turning scores to letter grades, and more

In summary: The loop should also maintain a total score for the section, an average score for the section, and a list of letter grades.
  • #1
uwp student
6
0

Homework Statement


We have multiple sections of a class for which we need to produce grading summaries by section. The grading summaries that we need are a count of how many students earned A’s, B’s, C's, etc., and the lowest, highest and average grade for each section. At the end of processing all sections the program will display a summary across all sections that will include the number of sections, the total number of scores and the average score across all sections.

Example:
Sample input 1
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
Sample output 1
2 80 97
Scores for section 1
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
Lowest Score: 80
Highest Score: 97
Average Score: 88.50

5 69 79 89 99 58

Scores for section 2
A's: 1
B's: 1
C's: 1
D's: 1
F's: 1
Lowest Score: 58
Highest Score: 99
Average Score: 78.80

7 60 70 80 90 100 0 59

Scores for section 3
A's: 2
B's: 1
C's: 1
D's: 1
F's: 2
Lowest Score: 0
Highest Score: 100
Average Score: 65.57


Total number of sections: 3
Total number of scores: 14
Class Average: 73.57

That's all the sections! Normal Termination.


The Attempt at a Solution



#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int score; // input variable

int section = 0;
int high_score = 0; // highest score of section
int low_score = 0; // lowest score of section
float average_section = 0; // average of all scores in the section
float average_score = 0; // average scores of class
int total_sections = 0; // total number of sections
int total_scores = 0; // total number of scores
int A_count = 0;
int B_count = 0;
int C_count = 0;
int D_count = 0;
int F_count = 0;


cout << fixed << showpoint << setprecision(2);
cin >>section;
cin >>total_scores;
cin >>score;

while (section > 0)
{
if ((score >= 90) && (score <= 100)) A_count++;
score++;
if ((score >= 80) && (score < 90)) B_count++;
score++;
if ((score >= 70) && (score < 80)) C_count++;
score++;
if ((score >= 60) && (score < 70)) D_count++;
score++;
if (score < 60) F_count++;

cout << "A's: " << A_count << endl;
cout << "B's: " << B_count << endl;
cout << "C's: " << C_count << endl;
cout << "D's: " << D_count << endl;
cout << "F's: " << F_count << endl;

cout << "Total number of scores: " << total_scores << endl;

cout << endl << "That's all the sections! Normal Termination." << endl;
}


This is as far as I can get so far, I do not know how to read mutiple sections, scores and I cannot get the loop to end. Any hints or advice would be great. Thanks.
 
Physics news on Phys.org
  • #2
uwp_student said:
Sample input 1
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
Sample output 1
2 80 97
Assuming for the moment that input is coming from the keyboard, you need to process an unknown number of sections. This raises a question that I will postpone.

For each section, the first number in the section is the count of scores in that section. Write a loop that executes the same number of times as the first number in the section. Each iteration of the loop should read the next test score, determine the letter grade for that score, determine whether that score is the new highest or lowest score of the section, and whether that score is the new highest or lowest score of all sections. The loop should also maintain a running total for the section scores and for all scores so that the section average and overall average can be computed.
 
  • #3
That is what I have been trying to do, I just do not know how to do that. (This is my first loop problem) So far I can get it to read the first number and that is all. But then it keeps reading it and will not stop until it says "Error, have exceed 10000 runs"
 
  • #4
I wouldn't use a while loop to process the data in a section - I would use a for loop, something like this.

Code:
cin << section_size;
for (i = 0; i < section_size; i++)
{
   // read a score
   // do other processing
}
 
  • #5
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int score; // input variable

int section = 0;
int high_score = 0; // highest score of section
int low_score = 0; // lowest score of section
float average_section = 0; // average of all scores in the section
float average_score = 0; // average scores of class
int total_sections = 0; // total number of sections
int total_scores = 0; // total number of scores
int A_count = 0;
int B_count = 0;
int C_count = 0;
int D_count = 0;
int F_count = 0;
int loop_count = 0;

cout << fixed << showpoint << setprecision(2);
cin >>section;
cin >>score;

loop_count = section;
while (loop_count == section)
{

if ((score >= 90) && (score <= 100)) A_count++;
score++;
if ((score >= 80) && (score < 90)) B_count++;
score++;
if ((score >= 70) && (score < 80)) C_count++;
score++;
if ((score >= 60) && (score < 70)) D_count++;
score++;
if (score < 60) F_count++;
score++;
total_scores = score + 1;
loop_count = loop_count + 1;

cout << "A's: " << A_count << endl;
cout << "B's: " << B_count << endl;
cout << "C's: " << C_count << endl;
cout << "D's: " << D_count << endl;
cout << "F's: " << F_count << endl;

cout << "Total number of scores: " << total_scores << endl;
cout << "Total number of sections: " << section << endl;


cout << endl << "That's all the sections! Normal Termination." << endl;

}

return 0;

}


This is what I have.
Here are the "hints" the teacher gave us:
• Work on getting your program to process one score.
• Once your program can process one score, add a loop to process all of the scores in one section.
• Once you program can process a section of data, add an outer loop to process multiple sections.
• Remember that several variables will need to be reset at the beginning of each section, but other variables , those for class statistics, should not be reset.
• ctrl-d will trigger an end of file on standard input.
• The Class Average is computed as the sum of all students’ scores divided by the number of students and is NOT the average of the section averages. Be careful declaring your variables as the correct type.


My program reads the first number and gives the correct number of sections and gives the correct letter grade for the first program. But i do not know how to make it read the next number.
 
  • #6
You should not read a score outside your loop (you read one score before the start of your while loop).

I would not use a while loop. See my example code.
 
  • #7
I do not understand your code. I am sorry for being difficult but this is only my second program and my first loop so I am having a lot of trouble understanding this
 
  • #8
The for loop is a counting loop that iterates a specified number of times. The while loop keeps executing as long as the initial condition is true.

In my for loop I have this line:
for(i = 0; i < section_size; i++)

i is the loop control variable. It is initialized to 0. As long as i < section_size, the loop body (the part in the braces, not shown here) will execute. After the loop body executes, the loop control variable i is incremented by 1.

Your book should have some discussion of the for loop and some examples.

In your sample input 1, you have
2 80 97

In my sample code, the 2 is read into section_size above my for loop. This means my for loop will execute two times - once to read in the 80 score and again to read in the 97 score.
Inside the body of the for loop is where you have your code to determine whether the particular score is an A, B, etc. and to do the other processing for a section of scores.
 
  • #9
it says "Error no value has been given to score"
 
  • #10
I do not think a for loop will work because it then looks like this when it compiles

2 88 92
A's: 0
B's: 1
C's: 0
D's: 0
F's: 0
Total number of scores: 0
Total number of sections: 2

That's all the sections! Normal Termination.
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
Total number of scores: 0
Total number of sections: 2

That's all the sections! Normal Termination.
 
  • #11
uwp student said:
I do not think a for loop will work because it then looks like this when it compiles
A for loop will definitely work, but you have semantic errors in your code.

You need two loops, one nested inside the other. The outer loop, which can be a while loop, processes each section. The inner loop, which should be a for loop, processes the scores within a section.

From your output, it looks like your code is reading the first item, 2, and treating it as the number of sections. This is incorrect. For the data you show, there is only one section. The 2 indicates the number of scores in that section.
uwp student said:
2 88 92
A's: 0
B's: 1
C's: 0
D's: 0
F's: 0
Total number of scores: 0
Total number of sections: 2

That's all the sections! Normal Termination.
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
Total number of scores: 0
Total number of sections: 2

That's all the sections! Normal Termination.
 
  • #12
BTW, to make your code more readable in this forum, place it between [ code] and [ /code] tags, without the leading spaces.
 
  • #13
I am not sure if you plan on being a programmer or not. But, you should really start by just writing out what you want to do in the form of comments in your IDE (I am assuming you are using an IDE).

If you do that, one step at a time, you will see why you want to use a for loop instead of a while loop.

Start with the requirements.

Write "pseudo" code (code that just says what you're going to do)

Code it up!
 
  • #14
Hello,
Well this has resurfaced a couple years later... Any help as to what I am doing wrong would be greatly appreciated. New to this, so be gentle, please. :)
Thank you, B

# include <iostream>
# include <iomanip>

using namespace std;

int main()
{
int score;
float averageClassScores;
int section = 0;
int hiScore = 0;
int loScore = 0;
int scoresInSection = 0;
int sectionNumber = 0;
int studentsInSection = 0;
int totalScores = 0;
int sumScores = 0;
int loop_count = 0;

cin >> score;

while(cin)
{
cout << "Scores for section: " << section << sectionNumber;

int A_count = 0;
int B_count = 0;
int C_count = 0;
int D_count = 0;
int F_count = 0;
int hiScore = -1;
int loScore = 101;

while(loop_count < scoresInSection)
{
cin >> scoresInSection;
if(score >= 90)
score++;
else if((score >= 80) && (score < 90))
score++;
else if((score >= 70) && (score < 80))
score++;
else if((score >= 60) && (score < 70))
score++;
else if(score <60)
score++;

if (hiScore < score)
hiScore = score;
if (loScore < score)
loScore = score;
loop_count++;
sumScores++;
}
cout << "Scores for section: " << section << endl;
cout << "A's: " << A_count << endl;
cout << "B's: " << B_count << endl;
cout << "C's: " << C_count << endl;
cout << "D's: " << D_count << endl;
cout << "F's: " << F_count << endl;
cout << scientific << noshowpoint;
cout << "Lowest Score: " << loScore << endl;
cout << "Highest Score: " << hiScore << endl;
cout << "Average Score: " << (totalScores/studentsInSection) << endl;
cout << fixed << showpoint << setprecision(2);
}
cout << "Total number of sections: " << sectionNumber << endl;
cout << "Total number of scores: " << sumScores << endl;
cout << "Class average: " <<averageClassScores << endl;
return 0;
}
 

Related to C++ question- turning scores to letter grades, and more

1. How do I convert numerical scores to letter grades in C++?

To convert numerical scores to letter grades in C++, you can use conditional statements such as if-else or switch-case to check the score and assign a corresponding letter grade. For example, if the score is between 90-100, you can assign it an A grade, between 80-89 a B grade, and so on.

2. Can I use a function to convert scores to letter grades in C++?

Yes, you can create a function that takes in the score as a parameter and returns the corresponding letter grade. This can make your code more organized and reusable.

3. How can I handle invalid input when converting scores to letter grades in C++?

You can use input validation techniques, such as checking if the input is within a valid range or if it is of the correct data type. You can also prompt the user to enter a valid input if the current input is not valid.

4. Is there a specific method for rounding scores before converting them to letter grades in C++?

C++ provides various rounding functions such as round(), floor(), and ceil(). You can use these functions to round the scores to the desired precision before converting them to letter grades.

5. Can I use a loop to convert multiple scores to letter grades in C++?

Yes, you can use a loop, such as a for loop or while loop, to convert multiple scores to letter grades. This can be useful when dealing with a large number of scores, as it eliminates the need to write the same code multiple times.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top