#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int numOfStudents;
cout << "Enter number of students: ";
cin>>numOfStudents; // read number of students
int arrID[numOfStudents]; // create array of integers for ID
double arrFirst[numOfStudents]; // create array of double for first marks
double arrSecond[numOfStudents]; // create array of double for second marks
double arrFinal[numOfStudents]; // create array of double for final marks
double arrTotal[numOfStudents]; // create array of double for total marks
char arrGrade[numOfStudents]; // create array of chars grades
double sum = 0; // sum of total marks
cout << "Input data for each student." << endl << endl;
for (int i = 0; i < numOfStudents; i++) {// for each student
cout << "Students " << i + 1 << ": " << endl;
cout << " ID: ";
cin >> arrID[i]; // read id
cout << " First mark: ";
cin >> arrFirst[i]; // read first mark
cout << "Second mark: ";
cin >> arrSecond[i]; // read second mark
cout << " Final mark: ";
cin >> arrFinal[i]; // read final mark
arrTotal[i] = arrFirst[i] + arrSecond[i] + arrFinal[i]; // calculate total mark
sum += arrTotal[i]; // add total mark to the sum
// compute grade
if (arrTotal[i] >= 90)
arrGrade[i] = 'A';
else if (arrTotal[i] >= 80)
arrGrade[i] = 'B';
else if (arrTotal[i] >= 70)
arrGrade[i] = 'C';
else if (arrTotal[i] >= 60)
arrGrade[i] = 'D';
else arrGrade[i] = 'F';
cout << endl;
}
cout << endl;
double average = sum / numOfStudents; // calculate average class mark
cout.setf(ios::fixed);
cout.precision(1); // set output precision
// print header of the table
cout << "-------------------------------------------------------------" << endl;
cout << setw(2) << "#" << setw(10) << "ID" << setw(8) << "First"
<< setw(8) << "Second" << setw(8) << "Final" << setw(8)
<< "Total" << setw(6) << "Grade" << endl;
cout << "-------------------------------------------------------------" << endl;
// print table of students with marks and grades
for (int i = 0; i < numOfStudents; i++) {
cout << setw(2) << i + 1 << setw(10) << arrID[i] << setw(8) << arrFirst[i]
<< setw(8) << arrSecond[i] << setw(8) << arrFinal[i] << setw(8)
<< arrTotal[i] << setw(6) << arrGrade[i] << endl;
}
cout << "-------------------------------------------------------------" << endl;
// print average class mark
cout << "Class average for " << numOfStudents << " students is: " << average << endl;
return 0;
}
Comments
Leave a comment