In: Computer Science
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output: Lab Quiz Exam Final
Student 1 ## ## ## ##
Student 2 ## ## ## ##
Student 3 ## ## ## ##
The final grade consists of 30% lab grade, 30% quiz grade and 40% exam. For example, if lab grade is 95, quiz grade is 82 and exam grade is 87, the final grade is equal to 95∗0.3+82∗0.3+87∗0.4. Use random numbers between 40 and 100 for lab, quiz and exam grades.
#include <bits/stdc++.h>
using namespace std;
int main() {
// create an array for the grades
float a[3][4];
// save the percentage of each grade to consider
float percent[3] = {0.3, 0.3, 0.4};
// the grades lie between this range
int max = 100, min = 40;
// fill the array with random numbers
for(int i=0; i<3; i++){
int finalGrade=0;
for(int j=0; j<3; j++){
// taking modulus with max-min will make the random number
generated lie between the
// given range and adding the min of the range gives the required
number
a[i][j] = rand()%(max-min + 1) + min;
// also calulate the final grade for each student
finalGrade += a[i][j]*percent[j];
}
// update the final grade
a[i][3] = finalGrade;
}
// manipulate the output format of the grades
cout<<setw(14)<<"Lab"<<setw(6)<<"Quiz"<<setw(5)<<"Exam"<<setw(6)<<"Final"<<endl;
// print the grades for each student
for(int i=0; i<3; i++){
cout<<"Student
"<<i<<setw(5)<<a[i][0]<<setw(5)<<a[i][1]<<setw(5)<<a[i][2]<<setw(5)<<a[i][3]<<setw(5)<<endl;
}
return 0;
}
Output: