I don't know what is the problem (Functions & Structures)

Hello
I have an assignment, I did it right but the last step won't work out, no idea why.

Here is the whole question
Part I:

Lab Exercise
Struct (Records)

a. Write the definition of a structure type named student that includes the following members: student ID, name, and an array of size five to represent students’ quiz grades.

b. Write a function named getData that takes a parameter X of type student. The function should read the data of X.


c. Write a function named printData that takes a parameter X of type student. The function should display the data of X.

d. Write a function named calculateAvg that takes a parameter X of type student. The function should calculate and return the average of X quiz grades.


e. Write a function named equal that takes two parameters X, and Y of type student. The function should return true if the Quiz array of X has the same content as the quiz grades of Y, otherwise the function should return false.

f. Write a main function to test your functions.



I have done this part, didn't face any problem.

Part II:

Struct (Records)

Complete the previous exercise to include the following:
a. Write the definition of a structure type named section, which includes the following components: section number (secNo) of type int, number of students in the section (noStudents) of type int, room number (roomNo) of type string, and an array (studentsList) of type student of size 20.

b. Write a function named getData that takes as parameter: mySection of type section. The function should prompt the user to enter the data of mySection.

c. Write a function named printData that takes as parameter mySection of type section. The function should print the data of mySection.

d. Write a function named averageQuiz1 that takes as parameter mySection of type section. The function should return the average of first quiz for all students in the section

e. Write a function named topStudents that take as parameter: mySection of type section. The function should print the students’ names with quiz1 score greater than the average of quiz1 for all students. (Hint: use averageQuiz1 function to get the average of quiz1)

f. Write a main program to test your functions.

I have done all of these points but my problem is with point e, there is no errors, and unable to figure out what's missing.

Here is my work
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <string>
using namespace std;
struct student{
	long studentID;
	string name;
	float Grades[5];
};

//Home Assignment Structure
struct section{
	int secNo;
	int noStudents;
	string roomNo;
	student studentsList[20];
};

void getData(student &X);
void printData(student X);
float calculateAvg(student X);
bool equal(student X, student Y);

//Home Assignment Functions Prototype.
void GetData(section &mySection);
void PrintData(section mySection);
float averageQuiz1(section mySection);
void topStudents(section mySection);

int main()
{
	float AVG;
	bool isEqual=false;
	student St1, St2;
	cout<<"Enter the student info:\n"
		<<"Student Name: ";
	getline(cin, St1.name);
	cout<<"Student ID: ";
	cin>>St1.studentID;
	cout<<"Student Grades(5): ";
	for(int i=0; i<5; i++)
	{
		cin>>St1.Grades[i];
	}

	getData(St1);
	printData(St1);
	AVG=calculateAvg(St1);
	cout<<"The average of quiz grades is: "<<AVG<<endl<<endl;

cout<<"Enter the 2nd student info:\n"
		<<"Student Name: ";
	getline(cin, St2.name);
	getline(cin, St2.name);
	cout<<"Student ID: ";
	cin>>St2.studentID;
	cout<<"Student Grades(5): ";
	for(int i=0; i<5; i++)
	{
		cin>>St2.Grades[i];
	}

	isEqual=equal(St1, St2);

	if(isEqual==true)
		cout<<"Same Grades\n"<<endl;
	else
		cout<<"Different Grades\n"<<endl;

	cout<<"**********\nHome Assignment\n**********\n"<<endl;
	section mySection;
	float QuizAv1=0;

	GetData(mySection);
	PrintData(mySection);
	QuizAv1=averageQuiz1(mySection);
	cout<<"\nThe average grade of first quiz for all students is: "
		<<QuizAv1<<endl<<endl;

	topStudents(mySection);



	system("pause");
	return 0;
}

void getData(student &X)
{
	student St2;

	St2.name=X.name;
	St2.studentID=X.studentID;
	for(int i=0; i<5; i++)
		St2.Grades[i]=X.Grades[i];
}
void printData(student X)
{
	cout<<endl<<"The data of student is:-"
		<<"\nName: "<<X.name<<endl
		<<"Student ID: "<<X.studentID<<endl<<"Grades: ";
	for(int i=0; i<5; i++)
	{
		cout<<X.Grades[i];
		if(i<5)
			cout<<"\t";
	}
	cout<<endl;
}
float calculateAvg(student X)
{
	float AV=0, SUM=0;
	for(int i=0; i<5; i++)
		SUM+=X.Grades[i];
	AV=SUM/5;
	return AV;
}
bool equal(student X, student Y)
{
	for(int i=0; i<5; i++)
	{
		if(X.Grades[i] != Y.Grades[i])
			return false;
	}
	return true;
}

//Home Assignment Functions
void GetData(section &mySection)
{
	cout<<"Enter the section number: ";
	cin>>mySection.secNo;
	cout<<"Enter the room name/number: ";
	getline(cin, mySection.roomNo);
	getline(cin, mySection.roomNo);
	cout<<"Enter the number of students: ";
	cin>>mySection.noStudents;
	cout<<"Enter the students details:\n";

	if(mySection.noStudents > 20)
		for(int i=0; i<20; i++)
		{
			cout<<i+1<<". "
				<<"Enter student name: ";
			getline(cin, mySection.studentsList[i].name);
			getline(cin, mySection.studentsList[i].name);
			cout<<"Enter student ID: ";
			cin>>mySection.studentsList[i].studentID;
			cout<<"Enter student grades (5): ";
			for(int j=0; j<5; j++)
				cin>>mySection.studentsList[i].Grades[j];
			cout<<endl<<endl;
		}
	else
		for(int i=0; i<mySection.noStudents; i++)
		{
			cout<<i+1<<". "
				<<"Enter student name: ";
			getline(cin, mySection.studentsList[i].name);
			getline(cin, mySection.studentsList[i].name);
			cout<<"Enter student ID: ";
			cin>>mySection.studentsList[i].studentID;
			cout<<"Enter student grades (5): ";
			for(int j=0; j<5; j++)
				cin>>mySection.studentsList[i].Grades[j];
			cout<<endl<<endl;
		}

}
void PrintData(section mySection)
{
	cout<<endl<<endl<<"***\nSection Data\n***\n"<<endl;

	cout<<"Section Number: "<<mySection.secNo<<endl
		<<"Room Number: "<<mySection.roomNo<<endl
		<<"Students Number: "<<mySection.noStudents<<endl
		<<"Students Detials:-\n"<<endl;
	if(mySection.noStudents>20)
		for(int i=0; i<20; i++)
		{
			cout<<i+1<<". ";
			cout<<"Name: "<<mySection.studentsList[i].name;
			cout<<"   ||    Student ID: "<<mySection.studentsList[i].studentID
				<<endl;
			cout<<" Student #"<<i+1<<" Grades: ";
			for(int j=0; j<5; j++)
			{
				cout<<mySection.studentsList[i].Grades[j];
				if(j<=4)
					cout<<"\t";
			}
			cout<<endl<<endl;
		}
	else
		for(int i=0; i<mySection.noStudents; i++)
		{
			cout<<i+1<<". ";
			cout<<"Name: "<<mySection.studentsList[i].name;
			cout<<"   ||    Student ID: "<<mySection.studentsList[i].studentID
				<<endl;
			cout<<" Student #"<<i+1<<" Grades: ";
			for(int j=0; j<5; j++)
			{
				cout<<mySection.studentsList[i].Grades[j];
				if(j<=4)
					cout<<"\t";
			}
			cout<<endl<<endl;
		}
}
float averageQuiz1(section mySection)
{
	float QuizAv1=0, SUM=0;
	if(mySection.noStudents>20)
	{
		for(int i=0; i<20; i++)
			SUM+=mySection.studentsList[i].Grades[0];
		QuizAv1=SUM/20;
		return QuizAv1;
	}
	else
	{
		for(int i=0; i<mySection.noStudents; i++)
			SUM+=mySection.studentsList[i].Grades[0];
		QuizAv1=SUM/mySection.noStudents;
		return QuizAv1;
	}
}
void topStudents(section mySection)
{
	cout<<"Students names who got marks above the average:\n";
	if(mySection.noStudents>20)
		for(int i=0; i<20; i++)
			if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
				cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];
	else
		for(int i=0; i<mySection.noStudents; i++)
			if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
				cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];

}


My problem with the last function.
I also tried to define a float variable in the function.
I did it like this (with changing the if & else statements) and didn't work
 
float AVG=averageQuiz1(mySection);


I know my work looks messy, but I hope someone got some time to help me :)
The logic of the code doesn't match the indentation in topStudents().

Let's indent it according to the meaning of the code:

1
2
3
4
5
6
7
8
9
10
11
12
void topStudents(section mySection)
{
    cout<<"Students names who got marks above the average:\n";
    if (mySection.noStudents>20)
        for (int i=0; i<20; i++)
            if (mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
                cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];
            else
                for(int i=0; i<mySection.noStudents; i++)
                    if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
                        cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];
}

But I guess this was the intended meaning:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void topStudents(section mySection)
{
    cout<<"Students names who got marks above the average:\n";
    if(mySection.noStudents>20)
    {
        for(int i=0; i<20; i++)
            if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
                cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];
    }
    else
    {
        for(int i=0; i<mySection.noStudents; i++)
            if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection))
                cout<<"Name: "<<mySection.studentsList[i].name<<"\tGrade: "<<mySection.studentsList[i].Grades[0];
    }
}


In this case the extra braces around the two parts of the if statement are required. It may be a good idea to include them as a matter of routine, where it may enhance the legibility, even if not affecting the meaning.

One benefit of including braces around sections of code is that amendments may easily be made, in particular the addition of extra cout messages for debugging purposes, without disturbing the flow of the program.

One other comment, it doesn't seem a very good idea to call the function averageQuiz1(mySection)) repeatedly in a loop (unless the value will be changing during the loop). In this case, I'd call it once and store the value in a variable.
Actually, looking at the code again, there is a particular type of design characteristic seen several times throughout this code. It goes like this:
1
2
3
4
5
6
7
8
9
10
if (noStudents > 20)
        for (int i=0; i<20; i++)
        {
            // Do Something (A)
        }
else
        for (int i=0; i<noStudents; i++)
        {
            // Do Something (B)
        }
But, both (A) and (B) are exactly the same. The only difference is in the loop termination condition.

It would be much better to simply code the loop once, with the appropriate condition. For example like this:
1
2
3
4
    for (int i=0; i<noStudents && i<20; i++)
    {
        // Do Something
    } 
or maybe like this...
1
2
3
4
5
    int num = (noStudents<20) ? noStudents : 20;
    for (int i=0; i<num; i++)
    {
        // Do Something
    }  
Topic archived. No new replies allowed.