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

AI Thread Summary
The discussion focuses on creating a C++ program to generate grading summaries for multiple class sections, including counts of letter grades, lowest, highest, and average scores. Participants emphasize the need for nested loops: an outer loop for processing each section and an inner loop for handling individual scores within those sections. There are challenges with reading input correctly and managing score counts, as well as confusion about using while versus for loops. Suggestions include initializing variables appropriately at the start of each section and using pseudo code to outline the program's logic before implementation. The conversation highlights the importance of understanding loop structures and input handling in programming.
uwp student
Messages
6
Reaction score
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
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.
 
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"
 
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
}
 
#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.
 
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.
 
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
 
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.
 
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;
}
 

Similar threads

Replies
3
Views
2K
Replies
9
Views
2K
Replies
1
Views
7K
Replies
36
Views
3K
Replies
23
Views
3K
Replies
7
Views
7K
Replies
13
Views
2K
Back
Top