For loop

I need to use a user define function to calculate grade distribution and display it along with starts.

My Program works but I don't know how to do the grade distribution using functions, I created bunch of for loops.


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include <iostream>
#include <string>

#include "Student.h"
using namespace std;

float avg(Student *obj, int size);
//string n_stars(Student *str, int numOfStudents);


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;
	}

	avg(s, numOfStudents);
	cout << "The class average is:" << avg(s, numOfStudents) << endl;
	//n_stars(s, numOfStudents);
	cout << "Grade Distribution:" << endl;

	cout << endl;
	cout << "0-9:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 0 && s[i].getGrade() <= 9)
			cout << "*";
	}

	cout << endl;
	cout << "10-19:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 10 && s[i].getGrade() <= 19)
			cout << "*";
	}

	cout << endl;
	cout << "20-29:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 20 && s[i].getGrade() <= 29)
			cout << "*";
	}
	cout << endl;
	cout << "30-39:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 30 && s[i].getGrade() <= 39)
			cout << "*";
	}
	cout << endl;
	cout << "40-49:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 40 && s[i].getGrade() <= 49)
			cout << "*";
	}
	cout << endl;
	cout << "50-59:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 50 && s[i].getGrade() <= 59)
			cout << "*";
	}

	cout << endl;
	cout << "60-69:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 60 && s[i].getGrade() <= 69)
			cout << "*";
	}
	cout << endl;
	cout << "70-79:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 70 && s[i].getGrade() <= 79)
			cout << "*";
	}
	cout << endl;
	cout << "80-89:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 80 && s[i].getGrade() <= 89)
			cout << "*";
	}
	cout << endl;
	cout << "90-99:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() >= 90 && s[i].getGrade() <= 99)
			cout << "*";
	}
	cout << endl;
	cout << "100:";
	for (int i = 0; i < numOfStudents; i++){

		if (s[i].getGrade() == 100)
			cout << "*";
	}

	cout << 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;*/

	


	system("pause");
	return 0;
}

float avg(Student obj[], int size){
	int subScore = 0;
	float avg = 0.0;

	for (int i = 0; i < size; i++){
		subScore += obj[i].getGrade();
	}

	avg = subScore / (float)(size);

	return avg;
}

//string n_stars(Student str, int numOfStudents){
//	
//} 
Can someone please help with this?
Topic archived. No new replies allowed.