### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240`` ``````#include #include 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: "<>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"<>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<>mySection.studentsList[i].studentID; cout<<"Enter student grades (5): "; for(int j=0; j<5; j++) cin>>mySection.studentsList[i].Grades[j]; cout<>mySection.studentsList[i].studentID; cout<<"Enter student grades (5): "; for(int j=0; j<5; j++) cin>>mySection.studentsList[i].Grades[j]; cout<20) for(int i=0; i<20; i++) { cout<20) { for(int i=0; i<20; i++) SUM+=mySection.studentsList[i].Grades[0]; QuizAv1=SUM/20; return QuizAv1; } else { for(int i=0; i20) for(int i=0; i<20; i++) if(mySection.studentsList[i].Grades[0] > averageQuiz1(mySection)) cout<<"Name: "< averageQuiz1(mySection)) cout<<"Name: "<

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:

 ``123456789101112`` ``````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: "< averageQuiz1(mySection)) cout<<"Name: "<

But I guess this was the intended meaning:
 ``12345678910111213141516`` ``````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: "< averageQuiz1(mySection)) cout<<"Name: "<

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:
 ``12345678910`` ``````if (noStudents > 20) for (int i=0; i<20; i++) { // Do Something (A) } else for (int i=0; i
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:
 ``1234`` `````` for (int i=0; i
or maybe like this...
 ``12345`` `````` int num = (noStudents<20) ? noStudents : 20; for (int i=0; i
Topic archived. No new replies allowed.