//Brandon Wade - bjwade1
#include <iostream>
#include <iomanip> // To enable use of setprecision
usingnamespace std;
int main()
{
// Variable declaration and initialization
int number_of_grades= 0; // Number of grades (input by user) to find average of
int grade_counter = 1; //Counter being used to keep track of grades
int number_of_students = 0; //Number of students to input grades for
int student_counter = 1; // Counter being used to track students
double student_grade_avg = 0; // Average of all student grades
double stugradeavg_total = 0; // Sum of all student_grade_avg
double class_avg = 0; // Average grade of class (to nearest hundredth)
double class_average_total = 0; // Sum of all students' grades
cout << "Please enter number of students: "; cin >> number_of_students; //User prompted to enter the number of students
cout << "Please enter number of grades per student: "; cin >> number_of_grades;
// The following while while loop will be ran from two user input counters. The first value will be the number of students.
//The second will be the number of grades to calculate for the number of students
while (student_counter <= number_of_students) // Beginning of while while loop
{
// if...else statement establishing letter grade guidelines follows
if (student_grade_avg >= 90)
cout << "letter grade: A";
elseif (student_grade_avg >= 80)
cout << "letter grade: B";
elseif (student_grade_avg >= 70)
cout << "letter grade: C";
elseif (student_grade_avg >= 60)
cout << "letter grade: D";
elseif (student_grade_avg < 60)
cout << "letter grade: F" ;
double student_grade_total = 0; // Sum of single student's grades (local variable)
while (grade_counter <= number_of_grades)// while - nested while loop to control adding user input number of grades
{
double grade = 0; // A single grade, input by user (local variable)
cout << endl << "Enter the student's grades: "; cin >> grade; // Prompt user to input the local variable grade
grade_counter = grade_counter + 1; //Progress the grade_counter by one
student_grade_total = student_grade_total + grade; // Assigning value to student_grade_total and increasing it by grade
student_grade_avg = student_grade_total / number_of_grades; // Calculating the student's average from the total of the input grades
cout << endl << "Student " << student_counter << " average: " << setprecision(2) << student_grade_avg << endl; // Outputting Student (number varies)'s grade to a hundredths place double
class_average_total = class_average_total + student_grade_avg; // Assigning value to class_average_total and increasing it by each student's average to use for calculation of class average
} // End nested while loop
student_counter = student_counter + 1; // Progress counter on main while loop by one
student_grade_total = 0; // Reinitialize student_grade_total to 0 for use on next student (if necessary)
student_grade_avg = 0; // Reinitialize student_grade_avg to 0 for use on next student (if necessary)
} //End main while loop
cout << endl << "Class average: " << class_average_total / number_of_students; // Calclate and display class average after line breaks
return 0;
} //end function main
Split line 21 into two lines after the semicolon- the only time a semicolon should have stuff following it is a for loop declaration.
Also, there is no need for line 56- if the while loop repeats, line 42 will be done again and make it 0.
Whenever you have a line like a = a + b; you can just do a += b; for identical functionality (48, 49, 52, 55).
Otherwise, looks good. Only criticism other than that would be your variable name on line 16: if it ends up crammed into an abbreviation like that, it might help to think of a shorter name.
student_counter = student_counter + 1; // Progress counter on main while loop by one
Even though this is an assignment for educational purposes, this is a prime example of how comments shouldn't really be used. Code should, as far as possible, be self explanatory, especially for simple statements that increment a variable for example. In my opinion, a simple...
student_counter++;
...is more than sufficient, although if the assignment specifically asks for heavily commented code, try to explain why the code does what it does, and not what it does. Comments, in my opinion, are best used to explain things that aren't immediately obvious or to describe the overall aim of code.
Didn't mean to put you down though. Your code is clear and your style is consistent. Good work.
You have a good start here, but I have some observations:
I don't believe you will ever have a grade other than "F" in the first loop:
Line 15:
double student_grade_avg = 0;
No increment or calculation of student_grade_avg occurs until line 50, in the inner loop, and it is recalculated for each iteration of this loop.
After the inner loop, in line 57, the student_grade_avg is again set to 0 before the outer loop continues, and before the if conditions are evaluated.
Perhaps lines 30 to 40 should be relocated between the inner loop block and line 55, I think. If you wanted an update to the letter grade per entry, place them as the last statements inside the inner loop block instead.
If you intend to print out the running average of each student's grades as entered, you need to use the counter grade_counter as your averaging divisor, and increment the counter after you calculate the average.
You initialized the grade_counter variable to 1 in line 12, but it never gets reset to iterate the inner loop, so the inner loop only runs once.
One more thing: the inner loop adds the student's average to class_average_total each iteration - this results in a mis-count of the number (divisor) you will need to calculate true class average. If you enter two grades for two students, it results in adding four student averages to the variable class_average_total, but you only divide it by two. . .You should either keep track of how many times you added a value to class_average_total, or move line 52 outside the inner loop block.
Thank you all for the replies. I should have began this post with: "This assignment has been turned it." I just wanted to clear that up.
@Ispil : Thanks. I'm new to programming, so it is good to have good practice recommendations. Also, thank you for the short hand suggestions, but, as I am still new to this, I am trying to engrain what the shorthand means before I start utilizing it. In reference to the names of my variables: I agree. This semester, my professor wants us to use very descriptive variable names so as to not get lost in our code. I believe that our variable names will not be graded nex semester.
@engr Bilal: Thank you for reading through it all. I will give this a try. I appreciate your concise assistance.
@denormal : I took no offense from what you said. You guessed it correct, though. My professor wants us to comment on most of our code to ensure we know WHY we are doing it, and we didn't just copy it from, say, a forum. Again, thank you for your recommendations. Also, as this is only the second program I have written, I am trying to avoid short hand until I can fully grasp what the short hand is saying by writing it out. Thank you for your suggestions.
@ PCrumley48: You have touched on a lot of issues I am having with the code. The first student's grades and average are calculating correctly, but my loop is only repeating once. As for the averages: I am trying to get an average for each student, then add up their three averages, using three for the divisor to find the average again. This is how the problem was presented. I will try out your suggestions when I get home. Thank you for your time.
...your professor grades you on variable names? I can only understand this if people have a silly habit of going through the alphabet procedurally before resorting to a1, a2, a3, a1b, a2b, a3b, and so forth- but those pieces of code will likely implode anyway.