I have code, but need comments. #include <iostream> #include <fstream> #include
ID: 663280 • Letter: I
Question
I have code, but need comments.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void getData(ifstream& inf, string n[], double tstData[][6], int count);
void calculateAverage(double tstData[][6], int count);
void calculateGrade(double tstData[][6], char gr[], int count);
void print(string n[], double tstData[][6], char gr[], int count);
int main()
{
string names[10];
double testData[10][6];
char grade[10];
ifstream inFile;
inFile.open("ch8_Ex13Data.txt");
if (!inFile)
{
cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, testData, 10);
calculateAverage(testData, 10);
calculateGrade(testData, grade, 10);
print(names, testData, grade, 10);
inFile.close();
system("PAUSE");
return 0;
}
void getData(ifstream& inf, string n[], double tstData[][6], int count)
{
for (int i = 0; i < count; i++)
{
inf >> n[i];
for (int j = 0; j < 5; j++)
inf >> tstData[i][j];
tstData[i][5] = 0.0;
}
}
void calculateAverage(double tstData[][6], int count)
{
double sum;
for (int i = 0; i < count; i++)
{
sum = 0.0;
for (int j = 0; j < 5; j++)
sum = sum + tstData[i][j];
tstData[i][5] = sum / 5;
}
}
void calculateGrade(double tstData[][6], char gr[], int count)
{
for (int i = 0; i < count; i++)
if (tstData[i][5] >= 90)
gr[i] = 'A';
else if (tstData[i][5] >= 80)
gr[i] = 'B';
else if (tstData[i][5] >= 70)
gr[i] = 'C';
else if (tstData[i][5] >= 60)
gr[i] = 'D';
else
gr[i] = 'F';
}
void print(string n[], double tstData[][6], char gr[], int count)
{
double sum = 0.0;
cout << left << setw(10) << "Name"
<< right << setw(8) << "Test 1"
<< setw(8) << "Test 2"
<< setw(8) << "Test 3"
<< setw(8) << "Test 4"
<< setw(8) << "Test 5"
<< setw(10) << "Average"
<< setw(8) << "Grade" << endl;
for (int i = 0; i < count; i++)
{
cout << left << setw(10) << n[i];
cout << right;
for (int j = 0; j < 5; j++)
cout << setw(8) << tstData[i][j];
cout << setw(10) << tstData[i][5]
<< setw(6) << gr[i] << endl;
sum = sum + tstData[i][5];
}
cout << endl << endl;
cout << "Class average: " << sum / count << endl;
}
Explanation / Answer
#include <iostream> //header file for input/output functions
#include <fstream> // header file for file functions
#include <string> // header file for string functions
#include <iomanip> //header file used to formatting and precession for floating values
using namespace std; // it is called as name space in c++ language
void getData(ifstream& inf, string n[], double tstData[][6], int count);// user defined function name is getData to read values
void calculateAverage(double tstData[][6], int count);// user defined function name is calculateAverage USED TO CALCULATE AVERAGE
void calculateGrade(double tstData[][6], char gr[], int count);// user defined function name is calculateAGrade USED TO CALCULATE GRADE
void print(string n[], double tstData[][6], char gr[], int count);// user defined function name is print used to display the data in the file
int main()// very c++ program start at main() function
{
// here we have declared three variables
string names[10];// variable type strring of 1D array
double testData[10][6]; // variable type double 2D array
char grade[10]; //variable of type char 1D array
ifstream inFile; //object of type ifstream
inFile.open("ch8_Ex13Data.txt"); //syntax to open a file (ie ch8_Ex13Data.txt)
if (!inFile) // this statment is used to check file exit or not if condition is true the 2 statments
{
cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, testData, 10);// function call is used to call the function body
calculateAverage(testData, 10); // function call is used to call the function body
calculateGrade(testData, grade, 10);// function call is used to call the function body
print(names, testData, grade, 10);// function call is used to call the function body
inFile.close();// syntax to close the file
system("PAUSE"); //system call
return 0;
}
// this function body is used to read the data from from file
void getData(ifstream& inf, string n[], double tstData[][6], int count)
{
for (int i = 0; i < count; i++)
{
inf >> n[i];
for (int j = 0; j < 5; j++)
inf >> tstData[i][j];
tstData[i][5] = 0.0;
}
}
// this function is used to calculate average
void calculateAverage(double tstData[][6], int count)
{
double sum;
for (int i = 0; i < count; i++)
{
sum = 0.0;
for (int j = 0; j < 5; j++)
sum = sum + tstData[i][j];
tstData[i][5] = sum / 5;
}
}
//this function is used to calculate grade for each
void calculateGrade(double tstData[][6], char gr[], int count)
{
for (int i = 0; i < count; i++)
if (tstData[i][5] >= 90)
gr[i] = 'A';
else if (tstData[i][5] >= 80)
gr[i] = 'B';
else if (tstData[i][5] >= 70)
gr[i] = 'C';
else if (tstData[i][5] >= 60)
gr[i] = 'D';
else
gr[i] = 'F';
}
// this function is used to display the content of texts and class average
void print(string n[], double tstData[][6], char gr[], int count)
{
double sum = 0.0;
cout << left << setw(10) << "Name"
<< right << setw(8) << "Test 1"
<< setw(8) << "Test 2"
<< setw(8) << "Test 3"
<< setw(8) << "Test 4"
<< setw(8) << "Test 5"
<< setw(10) << "Average"
<< setw(8) << "Grade" << endl;
for (int i = 0; i < count; i++)
{
cout << left << setw(10) << n[i];
cout << right;
for (int j = 0; j < 5; j++)
cout << setw(8) << tstData[i][j];
cout << setw(10) << tstData[i][5]
<< setw(6) << gr[i] << endl;
sum = sum + tstData[i][5];
}
cout << endl << endl;
cout << "Class average: " << sum / count << endl;
}
if u need in detail a explaination ping to me i vl give u and a clear explanation on paper
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.