Hi. I am a beginner to c++, and am taking an introductory class. I was tasked with making a program that calculates random numbers for students in a class, and displays the letter grades in total of all the students. Everything is working fine except one part, where i have a for loop nested inside a while loop. The for loop is supposed to generate ten random numbers and get the average, then display it for each individual student. However it isn't doing that. Suggestions?
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
//VARIABLES
int studentNum, finExam, midterm, progEx; //HOLD VALUES FOR NUMBER OF STUDENTS AND GRADE CATEGORIES
double progExAvg,
numericGrade; //HOLDS VALUE FOR NUMERIC GRADE OF EACH STUDENT
//counters
int totalProgEx = 0; //ACCUMULATOR FOR AVERAGE OF TOTAL PROGRAMMING EXCERCISES
int counter = 0; //COUNTER FOR NUMBER OF STUDENTS
int countA=0, //COUNTERS FOR EACH LETTER GRADE
countB=0,
countC=0,
countD=0,
countF=0;
//RANDOM GENERATOR FUNCTION
srand( time(0)); //This will ensure a really randomized number by help of time.
//USER PROMPT
cout << "Hello. Welcome to Random Grade generator." << endl;
cout << "Please enter how many students are in the class: " << endl;
cin >> studentNum;
// COMPUTES RANDOM GRADES
while(counter < studentNum)
{
//COMPUTES RANDOM GRADES FOR TEN PROGRAMMING EXCERCISES
for (int i=0; i< 10; i++)
{
progEx = rand()%100+1; // Randomizing the number between 1-100.
}
totalProgEx = totalProgEx + progEx;
progExAvg = totalProgEx/10;
midterm = rand()%100+1; // Randomizing the number between 1-100.
finExam = rand()%100+1; // Randomizing the number between 1-100.
counter++;
// DISPLAYS GRADES
cout << "\nStudent " << counter << " Grades\n" << endl;
cout << "Grade Average for 10 Programming Exercises: " << progExAvg << endl;
cout << "Grade for Midterm: " << midterm << endl;
cout << "Grade for Final exam: " << finExam << endl;
//COMPUTES NUMERIC GRADE
numericGrade = (progEx *.70) + (midterm*.15) + (finExam*.15);
//COMPUTES RANGES FOR LETTER GRADES
if ((numericGrade >= 0) && (numericGrade <= 59))
{
countF++;
}
elseif ((numericGrade >=60) && (numericGrade <= 69))
{
countD++;
}
elseif ((numericGrade >= 70) && (numericGrade <= 79))
{
countC++;
}
elseif ((numericGrade >= 80) && (numericGrade <=89))
{
countB++;
}
elseif ((numericGrade >=100) && (numericGrade <=90))
{
countA++;
}
}
// display class performance
cout << "\n\nNumber of students who received each letter grade:\n " << endl;
cout << countA << " A" << endl;
cout << countB << " B" << endl;
cout << countC << " C" << endl;
cout << countD << " D" << endl;
cout << countF << " F" << endl;
return 0;
}
Where did you store the programming exercise average?
1 2 3 4 5 6 7 8 9 10
//COMPUTES RANDOM GRADES FOR TEN PROGRAMMING EXCERCISES
for (int i=0; i< 10; i++)
{
progEx = rand()%100+1; // Randomizing the number between 1-100.
}
totalProgEx = totalProgEx + progEx;
progExAvg = totalProgEx/10;
Thanks, i fixed the what MiiNiPaa pointed out and the numeric grade calculation, but i'm not sure what you mean by where did i store the programming exercise average pheininger. I'm still having the problem where it is not correctly computing the program exercises average per student.