Use cout instead of printf Use cin instead of scanf A teacher of a class that co
ID: 3711603 • Letter: U
Question
Use cout instead of printf Use cin instead of scanf
A teacher of a class that contains 10 students; each studies 4 courses. The teacher wants to know the student of best mark in each course. Write a C++ program (using functions) that uses 2D arrays to help the teacher to get the required information. You should write functions to: 3, I. Read the 2D array (Students, Courses) values from the user (the teacher). 3.2. Find and display the maximum mark for each course. 3.3. Find and display student number that have maximum mark in each course.
Use cout instead of printf Use cin instead of scanf
A teacher of a class that contains 10 students; each studies 4 courses. The teacher wants to know the student of best mark in each course. Write a C++ program (using functions) that uses 2D arrays to help the teacher to get the required information. You should write functions to: 3, I. Read the 2D array (Students, Courses) values from the user (the teacher). 3.2. Find and display the maximum mark for each course. 3.3. Find and display student number that have maximum mark in each course.
Use cout instead of printf Use cin instead of scanf
A teacher of a class that contains 10 students; each studies 4 courses. The teacher wants to know the student of best mark in each course. Write a C++ program (using functions) that uses 2D arrays to help the teacher to get the required information. You should write functions to: 3, I. Read the 2D array (Students, Courses) values from the user (the teacher). 3.2. Find and display the maximum mark for each course. 3.3. Find and display student number that have maximum mark in each course.
Explanation / Answer
Below is your code....
#include<iostream>
#define NUM_STUDENTS 10 // number of students
#define NUM_COURSES 4 //number of courses
using namespace std;
//method to read the array(Students, Courses)
//passing array with pass by reference
void readScores(int (&arr)[NUM_STUDENTS][NUM_COURSES]) {
cout<<"Enter score of the students : "<<endl;
//loop to input array values.
for(int i = 0;i < NUM_STUDENTS; i++) {
for(int j = 0;j < NUM_COURSES; j++) {
cout<<"Score of Student #"<<(i+1)<<" in Course #"<<(j+1)<<": ";
cin>>arr[i][j];
}
}
}
//method to display the max scores per course
//passing array with pass by value
void displayMaxMarks(int arr[][NUM_COURSES]) {
int maxMarks = 0; // temporary variable
//loop to go through scores of each course
for(int i = 0;i < NUM_COURSES; i++) {
//temporary variable initial value is first core of the course
maxMarks = arr[0][i];
//loop to get max score per course
for(int j = 0;j < NUM_STUDENTS; j++) {
if(arr[j][i] > maxMarks) {
maxMarks = arr[j][i];
}
}
//printing the max score
cout<<"Maximum marks in Course #"<<(i+1)<<"is "<<maxMarks<<endl;
}
}
//method to display student with max marks per score.
void displayStudentWithMaxMarks(int arr[][NUM_COURSES]) {
//temp variable
int maxMarks = 0;
//loop to iterate through each course
for(int i = 0;i < NUM_COURSES; i++) {
maxMarks = arr[0][i]; //temp variable holding the marks of first student
int jMax = 0; // index of first student
//loop to iterate and get id and max marks of student per course
for(int j = 0;j < NUM_STUDENTS; j++) {
if(arr[j][i] > maxMarks) {
maxMarks = arr[j][i];
jMax = j;
}
}
//printing the results
cout<<"Student number with maximum marks in Course #"<<(i+1)<<" is "<<(jMax+1)<<" and he scored "<<maxMarks<<endl;
}
}
//main method
int main() {
//array to hold scores of each student in each course
int scores[NUM_STUDENTS][NUM_COURSES];
cout<<"Reading Score...."<<endl;
//reading scores from user
readScores(scores);
//displaying scores in matrix like format
cout<<"Scores array : ";
for(int i = 0;i < NUM_STUDENTS; i++) {
for(int j = 0;j < NUM_COURSES; j++) {
cout<<scores[i][j]<<" ";
}
cout<<endl;
}
//displaying the results
cout<<"Displaying maximum marks in each course...."<<endl;
displayMaxMarks(scores);
cout<<"Displaying student who scored maximum marks in each course...."<<endl;
displayStudentWithMaxMarks(scores);
return 0;
}
Output
Reading Score....
Enter score of the students :
Score of Student #1 in Course #1: 10
Score of Student #1 in Course #2: 11
Score of Student #1 in Course #3: 12
Score of Student #1 in Course #4: 13
Score of Student #2 in Course #1: 14
Score of Student #2 in Course #2: 15
Score of Student #2 in Course #3: 16
Score of Student #2 in Course #4: 17
Score of Student #3 in Course #1: 18
Score of Student #3 in Course #2: 19
Score of Student #3 in Course #3: 20
Score of Student #3 in Course #4: 30
Score of Student #4 in Course #1: 29
Score of Student #4 in Course #2: 28
Score of Student #4 in Course #3: 27
Score of Student #4 in Course #4: 26
Score of Student #5 in Course #1: 25
Score of Student #5 in Course #2: 24
Score of Student #5 in Course #3: 23
Score of Student #5 in Course #4: 22
Score of Student #6 in Course #1: 21
Score of Student #6 in Course #2: 1
Score of Student #6 in Course #3: 2
Score of Student #6 in Course #4: 3
Score of Student #7 in Course #1: 4
Score of Student #7 in Course #2: 5
Score of Student #7 in Course #3: 6
Score of Student #7 in Course #4: 7
Score of Student #8 in Course #1: 8
Score of Student #8 in Course #2: 9
Score of Student #8 in Course #3: 19
Score of Student #8 in Course #4: 18
Score of Student #9 in Course #1: 17
Score of Student #9 in Course #2: 16
Score of Student #9 in Course #3: 15
Score of Student #9 in Course #4: 14
Score of Student #10 in Course #1: 13
Score of Student #10 in Course #2: 12
Score of Student #10 in Course #3: 11
Score of Student #10 in Course #4: 10
Scores array :
10 11 12 13
14 15 16 17
18 19 20 30
29 28 27 26
25 24 23 22
21 1 2 3
4 5 6 7
8 9 19 18
17 16 15 14
13 12 11 10
Displaying maximum marks in each course....
Maximum marks in Course #1is 29
Maximum marks in Course #2is 28
Maximum marks in Course #3is 27
Maximum marks in Course #4is 30
Displaying student who scored maximum marks in each course....
Student number with maximum marks in Course #1 is 4 and he scored 29
Student number with maximum marks in Course #2 is 4 and he scored 28
Student number with maximum marks in Course #3 is 4 and he scored 27
Student number with maximum marks in Course #4 is 3 and he scored 30
--------------------------------
Process exited after 39.61 seconds with return value 0
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.