Problem Solving: Compiling Student Result
Use the multi-dimensional arrays to create a program that compiles the student results.
Compiling the result of the students
Write a program that compiles the result of students and displays it.
Problem statement
The program should take the following as input:
- Total number of students
- Total number of courses (assuming that every student has the same number of courses)
- Every student’s roll numbers, names, and obtained marks for each course
The program should:
- Compile the result and display the result with the roll number followed by the name and total marks of each student
- Display the class topper at the end
Sample program
Semester Detailed Result
______________________________________
# of students: 5
# of courses: 5
---------------------------
Roll# Name C1 C2 C3 C4 C5
---------------------------
1 Alice 90 75 80 60 91
2 Bob 94 70 80 70 97
3 Marie 80 87 90 97 98
4 Imran 80 60 70 70 55
5 Ali 85 86 87 90 98
Compiled Result!
______________________________________
Roll# Name Marks
----------------------------
1 Alice 396
2 Bob 411
3 Marie 452
4 Imran 335
5 Ali 446
The topper of the class is: Roll#3 Name: Marie Marks: 452
Try it yourself
Try to solve the above problem on your own. Write your code in the playground below
#include <iostream>using namespace std;int main() {// your code goes herereturn 0;}
Enter the input below
To test your playground code, you can copy the data given below.
1 Alice 90 75 80 60 912 Bob 94 70 80 70 973 Marie 80 87 90 97 984 Imran 80 60 70 70 555 Ali 85 86 87 90 98
Click the "Show Solution" button below to see the complete solution of approach 1.
Approach 1
Implementation guideline
Let’s solve the problem step by step.
Instruction: You should write or verify what you have coded in the playground above as you go through each step.
Step 1: Memory usage
Let’s start by initializing variables and arrays. We need the following variables:
const int noOfStudents = 5; // # of students recordsconst int noOfCourses = 5; // # of coursesint rows = noOfStudents; // noOfStudents maps to a number of rowsint cols = noOfCourses; // noOfCourses maps to number of columnsint RollNos[noOfStudents]={}; // for storing each roll#string Names[noOfStudents]; // for storing each namefloat StudentMarks[noOfStudents][noOfCourses]; // the marks of every student in each course.
Instruction: Add this memory part to the playground above.
Step 2: Loading data from the console
We will iterate through noOfStudents
times (for the user to enter all the records). The format is as follows: roll number followed by the name followed by noOfCourses
marks in each course.
// Displaying the end user directions for entering records.cout << "\tEnter semester records of "<<noOfStudents<<" students\n"<<endl;cout << "\tFormat:"<<"Roll#\tName\t";for(int c=0; c<noOfCourses; c++)cout <<"C"<<c+1<<'\t';cout <<"\n\n";// Entering recordsfor(int r=0; r<noOfStudents; r++){cout <<"Record# "<<r+1<<".\t";cin>>RollNos[r];cin>>Names[r];for(int c=0; c<noOfCourses; c++){cin >> StudentMarks[r][c];}}
Step 3: Result compilation and printing
We want to calculate each student’s total marks and maintain the top student record.
-
To maintain the top student marks, we have the
maxMarks
,maxRollNo
, andmaxName
variables. -
To sum the marks of each course, we have a
total
variable. -
Inside the loop, we will sum the marks of each student course, compare each student’s marks, and update the
maxMarks
,maxRollNo
, andmaxName
based on current marks. if the current marks are maximum, then the previous marks update the student record.
int maxMarks = -1;int maxRollNo;string maxName;float total = 0;cout << "Compiled Result...!!!\n\n";cout << "\t---------------------\n";for(int r=0; r<noOfStudents; r++){cout << RollNos[r]<<"\t"<<Names[r]<<": \t";total = 0;// This initialization is needed here otherwise the 2nd student marks// and the first students marks will be accumulated in total.for(int c=0; c<noOfCourses; c++){total = total + StudentMarks[r][c];}cout << total << endl;// Maintaining the maximum recordif(total>maxMarks){maxMarks = total;maxRollNo = RollNos[r];maxName = Names[r];}}cout << "The topper of the class is : Roll#"<<maxRollNo<< " Name: "<<maxName <<" Marks: "<<maxMarks<<endl;
We will see how the functional approach will make the step-by-step implementation much cleaner.
Approach 2: Using functional and loading data
This time, we have Result.txt
file in which we have the following data:
5 5
1 Alice 90 75 80 60 91
2 Bob 94 70 80 70 97
3 Marie 80 87 90 97 98
4 Imran 80 60 70 70 55
5 Ali 85 86 87 90 98
Here 5 5
means there are five records each having five courses.
Try it yourself
Here’s the playground where you should write the code using the functional approach per the guidance below:
#include <iostream>#include <fstream>using namespace std;#define maxMarksCount 50#define maxRecords 50int main(){// Code here!return 0;}
During the functional implementation, we should remember that we will try to decouple the compilation and printing processes as much as possible. How can we do that?
Let’s look into the detailed implementation.
Implementation steps to follow
Here’s the main flow of the program.
Step 0: Memory requirement
Before looking at the hint, figure out the variables and arrays you will need to code in the functional implementation.
Step 1: Loading the records from the file
Look at the Result.txt
file and figure out which variables we need in this step. Write a function recordLoader()
, which should load the entire file along with all the parameters on which the entire program should be working.
Step 2: Printing the loaded data
This is usually a good practice for a developer and end user to display whatever data has been loaded into the program’s memory. The developer needs it because they should be sure the data is loaded properly and ready for processing, and the end user should also see it because it looks nice on the console.
Let’s make the function, printRecord()
, which will print the record. Figure out what variables we need to pass so that we should be able to print the following:
Semester Detailed Result
______________________________________
# of students: 5
# of courses : 5
---------------------------
Roll# Name C1 C2 C3 C4 C5
---------------------------
1 Alice 90 75 80 60 91
2 Bob 94 70 80 70 97
3 Marie 80 87 90 97 98
4 Imran 80 60 70 70 55
5 Ali 85 86 87 90 98
Step 3: Compiling the result
This is the most important step involving computing the compiled total marks of each student as well as it should find out who is the student who got the maximum marks.
To accomplish this step, we’ll make a function compileResult()
. Think before looking at the solution and what the arguments are on which this function should perform.
Step 4: Displaying the compiled result
After compiling the result, we need to display the final result with the student who obtained the maximum marks.
For this, let’s make the function that should display the following output:
Compiled Result!
______________________________________
Roll# Name Marks
----------------------------
1 Alice 396
2 Bob 411
3 Marie 452
4 Imran 335
5 Ali 446
The topper of the class is: Roll#3 Name: Marie Marks: 452
Final step: complete the main function
Now that we’ve implemented all the functions, we should test each function one by one. The main()
function should like the following:
Let’s look at the complete implementation:
5 51 Alice 90 75 80 60 912 Bob 94 70 80 70 973 Marie 80 87 90 97 984 Imran 80 60 70 70 555 Ali 85 86 87 90 98
Exercise: Print maximum marks for the specified course
You must be wondering why we used arrays to read data from the file and then process and store it, whereas we could've also processed the data line by line and output the result without storing it. The reason is simple—we stored the results in an array, allowing us to write functions to query the data.
In this exercise, you'll have to implement a function, computeHighestMarksForCourse()
, which takes the course index for which the highest score is to be returned. Utilize the playground below for your implementation. And if you get stuck, feel free to look at the solution.
5 5 1 Alice 90 75 80 60 91 2 Bob 94 70 80 70 97 3 Marie 80 87 90 97 98 4 Imran 80 60 70 70 55 5 Ali 85 86 87 90 98