I would like to compute the weighted average of test 1 ( 30%) , test 2( 30%) and
ID: 3875971 • Letter: I
Question
I would like to compute the weighted average of test 1 ( 30%) , test 2( 30%) and final (40%) and fill it on the right column that when compiled and run says " the average grade of student #". How would I go about doing this? btw, this is c++
Please and thank you.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i = 30, j = 3;
int total = 0, avg = 0;
int c1430[30][3];
//displaying the grades
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 3; j++)
{
c1430[i][j] = rand() % (100);
}
}
//displaying the students
cout << "Exam 1 Exam 2 Final Exam" <<endl;
cout << "-----------------------" << endl;
for (int i = 0; i < 30; i++)
{cout << "Student#" << i + 1 << ":";
for (int j = 0; j < 3; j++)
{
cout << c1430[i][j] << " ";
}
{
total = total + c1430[i][j];
avg = total / 3;
}
cout << "The Average Grade of Student#" << i + 1 << ":" << avg << endl;
total = 0;
cout << endl;
}
return 0;
}
Explanation / Answer
Your code was almost right . here your total should be with in the for loop because total need 3 values in your case it only get one value ie c1430[i][j - max] thats why you dont get right avg
and i hop you have great skill in program here is the program
one more sugestion please try to write code in a good format
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i = 30, j = 3;
int total = 0, avg = 0; // make avg to float to get fractional
int c1430[30][3];
//displaying the grades
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 3; j++)
{
c1430[i][j] = rand() % (100);
}
}
//displaying the students
cout << "Students -xam 1 -xam 2 Final -xam" <<endl; // just rearrange sorry for that (-) can't uset -xam for me
cout << "-------------------------------------------------" << endl;
for (int i = 0; i < 30; i++)
{
cout << "Student#" << i + 1 << ": ";
for (int j = 0; j < 3; j++)
{
cout << c1430[i][j] << " ";
total = total + c1430[i][j]; // i moved your this line to here
}
{ // these are not compersary no need just remove it, no change in program
// total = total + c1430[i][j]; early
avg = total / 3;
} // no effect
cout << "The Average Grade of Student#" << i + 1 << ":" << avg << endl;
total = 0;
cout << endl;
}
return 0;
}