write a 2D array to show average of 5 subjects for 20 students
#include <iostream>
using namespace std;
void readData(int studentsMarks[20][5]) {
for (int i=0; i<20; i++) {
cout << "Enter marks for " << i+1 << "-th student: ";
for (int j=0; j<5; j++) {
cin >> studentsMarks[i][j];
}
}
}
void printAverage(int studentsMarks[20][5]) {
int sum;
double avg;
for (int i=0; i<20; i++) {
sum = 0;
for (int j=0; j<5; j++) {
sum += studentsMarks[i][j];
}
avg = sum / 5.0;
cout << "Average mark for student " << i+1 << " is " << avg << endl;
}
}
int main() {
int studentsMarks[20][5];
readData(studentsMarks);
printAverage(studentsMarks);
return 0;
}
Comments
Leave a comment