C++ program: Based on the program below, how do you calculate the average of the
ID: 3877117 • Letter: C
Question
C++ program: Based on the program below, how do you calculate the average of the students’ grades in the fourth column by using two dimensional array? Please show the code. Thanks.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string names[4];
int grades[4][4];
int count = 0;
ifstream namesFile;
// Open the input file that contains the students names.
namesFile.open("StudentNames.txt");
// Check if the input file is open.
if (namesFile.is_open())
{
// Run a loop to read the file until end of the file is reached
while (!namesFile.eof())
{ // Read the names into the array.
namesFile >> names[count];
count++;
}
namesFile.close(); // Close the input file.
ifstream gradesFile;
// Open the file that contains grades
gradesFile.open("StudentGrades.txt");
// Check if the file is open.
if (!gradesFile)
cout << "Error opening data file ";
else
{ // Run a loop and read the grades into the array.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
gradesFile >> grades[i][j];
}
}
gradesFile.close(); //close the input file
}
// Run a loop to print the names to the output screen
for (int i = 0; i < 4; i++)
{
cout << names[i];
// Run a loop to print the grades of each student
for (int j = 0; j < 3; j++)
{
cout << " " << grades[i][j] << " ";
}
cout << endl;
}
}
system("pause");
return 0;
}
Ifstream
StudentNames.txt
James
John
Jonas
Joel
StudentGrades.txt
100 87 72
73 65 45
100 45 70
48 80 89
Explanation / Answer
Modified query to get average of students
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string names[4];
int grades[4][4];
int count = 0;
ifstream namesFile;
// Open the input file that contains the students names.
namesFile.open("StudentNames.txt");
// Check if the input file is open.
if (namesFile.is_open())
{
// Run a loop to read the file until end of the file is reached
while (!namesFile.eof())
{ // Read the names into the array.
namesFile >> names[count];
count++;
}
namesFile.close(); // Close the input file.
ifstream gradesFile;
// Open the file that contains grades
gradesFile.open("StudentGrades.txt");
// Check if the file is open.
if (!gradesFile)
cout << "Error opening data file ";
else
{ // Run a loop and read the grades into the array.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
gradesFile >> grades[i][j];
}
}
gradesFile.close(); //close the input file
}
//declaring variables to find average
double total,average;
// Run a loop to print the names to the output screen
for (int i = 0; i < 4; i++)
{
cout << names[i];
// Run a loop to print the grades of each student
//Adding average function to code
for (int j = 0; j < 3; j++)
{
//totals of student
total += grades[i][j];
cout << " " << grades[i][j] << " ";
}
//average of student
average = total/3;
//grades of each student and average of each student
cout << " "<< names[i]<< " "<<" "<<"average:"<<average<<endl;
average = 0;
total = 0;
}
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.