LECTURE 22
Md. Minhaz Akram
EEE 1109
Practical Example Of 2D Array with Functions.
In C++, an array is a collection of elements of the same data type stored in contiguous memory locations.
Arrays allow you to store multiple values in a single variable, making it easier to work with collections of data.
This C++ code is designed to track student scores across multiple categories (Class Test, Mid Term, Final Exam) and
compute the roll number of the student who achieved the highest score in each category, as well as in total. Here's a
breakdown of how the code works:
#include<iostream>
using namespace std;
// The following function will take the data matrix and category[column]
// and will return the roll number who got the highest
int maxRoll(int data[][5],int N,int C)
{
int id=0, max=data[0][C];
for(int i=1;i<N;i++)
{
if(data[i][C]>max)
{
max= data[i][C];
id= i;
}
}
return data[id][0]; // returning the roll number who got the highest
}
// The following function will set the matrix[dataset]
void setData(int data[][5], int N)
{
int sum=0;
cout << "/nThere are five columns and "<< N << " rows:"<<endl;
for(int r=0;r<N;r++)
{
sum=0;
for(int c=0;c<4;c++)
{
cout <<"Enter [ "<< r << " ][ "<< c<< " ]:";
cin >> data[r][c];
if(c==0)
continue;
sum+=data[r][c];
}
data[r][4]=sum;
cout<<"Student having id: "<< data[r][0]<<" got the total: "<<data[r][4]
<<endl;
}
}
int main()
{
int data[50][5]= {0},N;
Page 1 of 3
cout << "Enter the number of students:";
cin >> N;
cout<< "The columns are denoted as follows:"<<endl;
cout << "Roll \t ClassTest \t Mid \t Final \t Total \n";
setData(data,N);
cout << "The student roll who got the highest in ClassTest:"<<
maxRoll(data,N,1)<<endl;
cout << "The student roll who got the highest in MidTerm:"<<
maxRoll(data,N,2)<<endl;
cout << "The student roll who got the highest in Final:"<<
maxRoll(data,N,3)<<endl;
cout << "The student roll who got the highest in Total:"<<
maxRoll(data,N,4)<<endl;
return 0;
}
Enter the number of students:3
The columns are denoted as follows:
Roll ClassTest Mid Final Total
/nThere are five columns and 3 rows:
Enter [ 0 ][ 0 ]:23
Enter [ 0 ][ 1 ]:7
Enter [ 0 ][ 2 ]:8
Enter [ 0 ][ 3 ]:42
Student having id: 23 got the total: 57
Enter [ 1 ][ 0 ]:56
Enter [ 1 ][ 1 ]:8
Enter [ 1 ][ 2 ]:7
Enter [ 1 ][ 3 ]:37
Student having id: 56 got the total: 52
Enter [ 2 ][ 0 ]:98
Enter [ 2 ][ 1 ]:2
Enter [ 2 ][ 2 ]:2
Enter [ 2 ][ 3 ]:50
Student having id: 98 got the total: 54
The student roll who got the highest in ClassTest:56
The student roll who got the highest in MidTerm:23
The student roll who got the highest in Final:98
The student roll who got the highest in Total:23
--------------------------------
Process exited after 47.45 seconds with return value 0
Press any key to continue . . .
2. maxRoll Function
Parameters:
data: A 2D array representing student scores and roll numbers.
N: The number of students (rows in the array).
C: The column index of the category you want to find the highest score in (1 for ClassTest, 2 for
MidTerm, 3 for Final, 4 for Total).
Logic:
This function finds the roll number of the student who got the highest score in the category represented
by column C.
Page 2 of 3
Initially, the maximum score is set to the score of the first student in the specified category
(data[0][C]), and the id variable keeps track of the index of the student with the highest score.
A loop iterates over all students, comparing each student's score in the category (data[i][C]) to the
current maximum. If a higher score is found, max is updated, and id is set to the current student's index.
Finally, the function returns the roll number of the student with the highest score, which is stored in
data[id][0] (the first column contains the roll number).
3. setData Function
Parameters:
data: A 2D array where each row represents a student's data.
N: The number of students (rows).
Logic:
This function sets up the student data by taking input from the user for each student.
It prompts the user to enter values for each student's roll number, Class Test, MidTerm, and Final
Exam scores.
For each student (r), the function iterates through columns 0 to 3 to take input. The roll number is stored
in data[r][0], and the scores are stored in data[r][1], data[r][2], and data[r][3].
The total score is calculated by summing the Class Test, MidTerm, and Final Exam scores, and it is
stored in data[r][4].
After the data for each student is entered, the function prints out the total score for the student.
4. main Function
Logic:
o An array data[50][5] is declared, which can hold up to 50 students, with 5 columns for each
student's roll number, Class Test, MidTerm, Final, and Total score.
o The program asks the user to input the number of students (N).
o The column headings are printed out for clarity.
o The setData() function is called to collect and calculate the students' scores.
o After the data is collected, the maxRoll() function is called four times to determine which
student (by roll number) got the highest score in each of the four categories: Class Test,
MidTerm, Final, and Total.
o The results are printed out to the screen.
Key Points:
The program stores student data (roll number and scores) in a 2D array data[50][5], where each row
represents a student.
For each student, their total score is computed and stored in the 5th column (data[r][4]).
The program then uses the maxRoll() function to identify the roll number of the student who achieved
the highest score in each category.
Page 3 of 3