0% found this document useful (0 votes)
31 views

3 Students, Each With 4 Grades (Assignments) - Input Their Data and Determine Each Student's Avg As Well As The Class Avg in Each Assignment

This C++ program uses a 2D array to store grades for 3 students over 4 assignments. It initializes the array, inputs the grade data, calculates the average grade for each student and for each assignment, and prints the final grade array including student and class averages.

Uploaded by

Randy Sooknanan
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
31 views

3 Students, Each With 4 Grades (Assignments) - Input Their Data and Determine Each Student's Avg As Well As The Class Avg in Each Assignment

This C++ program uses a 2D array to store grades for 3 students over 4 assignments. It initializes the array, inputs the grade data, calculates the average grade for each student and for each assignment, and prints the final grade array including student and class averages.

Uploaded by

Randy Sooknanan
Copyright
© © All Rights Reserved
You are on page 1/ 2

3 students, each with 4 grades (assignments).

Input their data and determine each


student’s avg as well as the class avg in each assignment.

/*2-Dimensional Arrays*/

# include <iostream>
using namespace std;

const int rows=4;


const int cols=5;

void initialize (double grades[][cols])


{
for(int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
grades[i][j]=0;
}

void read_array(double grades[][cols])


{
for (int i=0;i<=rows-2;i++)
{
cout<<"Enter grades for student "<<i+1<<"\n";
for(int j=0;j<=cols-2;j++)
cin>>grades[i][j];
}
cout<<"\n";
}

void students_avg(double grades[][cols])


{
for(int i=0;i<=rows-2;i++)
{
for (int j=0;j<=cols-2;j++)
grades[i][cols-1]+=grades[i][j];
grades[i][cols-1]/=(cols-1);
}
}

void class_avg(double grades[][cols])


{
for(int j=0;j<cols-1;j++)
{
for(int i=0;i<rows-1;i++)
grades [rows-1][j]+=grades[i][j];
grades[rows-1][j]/=(rows-1);
}
}

void print_array(double grades[][cols])


{
cout<<"\t\tGrades\t\t\t\tAverage\n"<<endl;
for (int i=0;i<=rows-2;i++)
{
cout<<"Student"<<i+1<<"\t";
for(int j=0;j<cols;j++)
cout<<grades[i][j]<<"\t";
cout<<endl;
}
cout<<"\nAverage\t\t";
for (int j=0;j<=cols-2;j++)
cout<<grades[rows-1][j]<<"\t";
cout<<endl;
}

int main()
{
double grades[rows][cols];
initialize(grades);
read_array(grades);
students_avg(grades);
class_avg(grades);
print_array(grades);
cout<<endl;
system("Pause");
return 0;
}

You might also like