Grade Distribution
Apr 17, 2015 at 12:26am UTC
One last part of my program is to do grade distribution and display it along with starts.
I have no idea how to do this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
//float avg(Student *obj, int size);
int main(){
int numOfStudents;
cout << " Please enter the number of students " << ":" ;
cin >> numOfStudents;
Student *s = new Student[numOfStudents];
cout << "Please enter student data" << ":" << endl;
string studentName;
int studentGrade = 0;
for (int i = 0; i < numOfStudents; i++){
cout << "Name " << ": " ;
cin >> studentName;
cout << "Grade " << ":" ;
cin >> studentGrade;
s[i].setName(studentName);
s[i].setGrade(studentGrade);
cout << s[i].getName() << endl;
cout << s[i].getGrade() << endl;
}
int sub=0; float avg = 0;
for (int i = 0; i < numOfStudents; i++){
sub += s[i].getGrade();
}
avg = sub / (float )(numOfStudents);
cout <<"Class average:" << avg << endl;
cout << endl;
int score = 0;
for (int i = 0; i < numOfStudents; i++){
score = s[i].getGrade();
cout << "Grade distribution:" << endl;
cout << " 0-9:" << endl;
cout << "10-19:" << endl;
cout << "20-29:" << endl;
cout << "30-39:" << endl;
cout << "40-49:" << endl;
cout << "50-59:" << endl;
cout << "60-69:" << endl;
cout << "70-79:" << endl;
cout << "80-89:" << endl;
cout << "90-99:" << endl;
cout << "100:" << endl;
Apr 17, 2015 at 12:53am UTC
What output are you expecting after grade distribution?
Apr 17, 2015 at 1:10am UTC
It should look something like
Grade distribution:
0-9: *
10-19:
20-29:
30-39: *
40-49: *
50-59:
60-69: ***
70-79: ****
80-89: *********
90-99: ***********
100: **
What you need to do is
1) Pay attention to the spaces after the colon before the stars start
2) Write a function that returns a string of n stars.
How you manage the distribution is up to you. You can, if you want to leave it to just have the same number of stars printed as there are students.
cout << "70-79: " << n_stars( count_students_with_grade_in( s, numOfStudents, 70, 79 ) ) << "\n" ;
You'll need two functions to help, of course:
int count_students_with_grade_in( Student* s, int n, int min_grade, int max_grade );
(I presume a student's grade is an
int . If it is not, change the min/max grade thing to the proper data type.)
Hope this helps.
Last edited on Apr 17, 2015 at 1:11am UTC
Apr 17, 2015 at 2:23am UTC
I did it without using functions(I have to use function tho), it kind of works but the spacing is off.
Here's what I did:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
cout << "0-9:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 0 && s[i].getGrade() <= 9)
cout << "*\n" ;
}
cout << "10-19:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 10 && s[i].getGrade() <= 19)
cout << "*\n" ;
}
cout << "20-29:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 20 && s[i].getGrade() <= 29)
cout << "*\n" ;
}
cout << "30-39:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 30 && s[i].getGrade() <= 39)
cout << "*\n" ;
}
cout << "40-49:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 40 && s[i].getGrade() <= 49)
cout << "*\n" ;
}
cout << "50-59:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 50 && s[i].getGrade() <= 59)
cout << "*\n" ;
}
cout << "60-69:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 60 && s[i].getGrade() <= 69)
cout << "*\n" ;
}
cout << "70-79:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 70 && s[i].getGrade() <= 79)
cout << "*\n" ;
}
cout << "80-89:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 80 && s[i].getGrade() <= 89)
cout << "*\n" ;
}
cout << "90-99:\n" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() >= 90 && s[i].getGrade() <= 99)
cout << "*\n" ;
}
cout << "100:" ;
for (int i = 0; i < numOfStudents; i++){
if (s[i].getGrade() == 100)
cout << "*" ;
}
cout << endl;
I'm gonna try your way.
Thanks!!
Apr 17, 2015 at 2:30am UTC
What you've got is very good. Just notice that when you find yourself doing the same thing over and over, that thing is a good candidate for a function.
Apr 17, 2015 at 2:45am UTC
That's, right. I just don't know how use my if statements inside the function.
Apr 17, 2015 at 4:41am UTC
The values that are different should be arguments to the function.
Topic archived. No new replies allowed.