Have a .txt document as:
String
int
int
int
int
int
int
int
int
int
int
String
int
int
...
for 15 strings. Looking to input the names into a string array then the grades into a 2d array Then output with some other calculations. Here is what i have so far:
#include <conio.h>
#include <iostream> // required to perform c++ stream I/O
#include <fstream> // required
#include <cstdlib>
#include <cmath> // required to perfrom math functions in C++ library
#include <iomanip> // required for parameterized stream manipulators
#include <string> // required to access string functions
using namespace std; // for accessing c++ standard library members
// define constants
const int TESTS = 10; // number of tests per student
const int STUDENTS = 15; // number of students
// function main begins program execution
int main()
{
// define two dementional array to store student grades
int data[STUDENTS][TESTS] = {0};
string names[STUDENTS] = {0};
ifstream inputFile;
inputFile.open("GradeSpring06.txt"); // open file to read from
if (!inputFile)
{
cout << "Error opening file!" << endl;
exit( 1 );
}
for (int student=0; student< STUDENTS ; student++)
{
inputFile >> names[student];
for (int test=0; test<TESTS;test++)
{
inputFile >> data[student][test]; // set file
}
}
cout <<fixed <<setprecision(1);
double testTotal = 0.0;
double classTotal = 0.0;
double squared = 0.0;
double var = 0.0;
double sdev = 0.0;
int mxm = data[0][0];
int min = data[0][0];
// display a header
cout << "\nGrade summary" << endl;
cout << "--------------" << endl;
cout << left << setw(7) << "Student"
<< right << setw(5) << right << "Ex1"
<< setw(7) << "Ex2"
<<setw(6) << "Ex3"
<<setw(6) << "Qz1"
<<setw(5) << "Qz2"
<<setw(6) << "Qz3"
<<setw(6) << "Qz4"
<<setw(6) << "Qz5"
<<setw(6) << "Pr1"
<<setw(6) << "Pr2"
<<setw(8) <<"Averg" <<endl;
for ( int student = 0; student < STUDENTS; student ++)
{
cout << left <<setw(7) << student + 1;
for ( int test = 0; test < TESTS; test++)
{
// display student grades
int grade = data[student][test];
cout << right <<setw(5) << grade <<" ";
classTotal += grade;
}
// find average of students grades
double average =(((data[student][0]+data[student][1]+data[student][2])/3)*0.4)+(((data[student][8]+data[student][9])/2)*0.3)+(((data[student][3]+data[student][4]+data[student][5]+data[student][6]+data[student][7])/5)*0.3);
cout << right << setw(7) << average << endl;
}
cout << "\n" << left << setw(7) << "Test Avg";
for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
testTotal += data[student][test];
}
cout << right << setw(6) << testTotal / STUDENTS;
testTotal = 0.0;
}
cout << endl;
cout << left << setw(7) << "Stan dev";
for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
squared += pow(data[student][test], 2.0);
var = (squared - ((testTotal * testTotal) / 15) ) / 15;
sdev = sqrt(var);
}
cout << right << setw(6) << sdev;
sdev = 0.0;
}
cout << endl;
cout << left << setw(7) << "Max";
for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
if (data[student][test] > mxm)
{
mxm = data[student][test];
}
}
cout << right <<setw(6) << mxm;
mxm = 0;
}
cout <<endl;
cout << left << setw(7) << "Min";
for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
if (data[student][test] < min)
{
min = data[student][test];
}
}
cout << right <<setw(6) << min;
min = 100;
}
cout <<endl;
double classAverage = classTotal / (STUDENTS * TESTS);
cout << "\nClass average: " <<classAverage <<endl <<endl;
getch();
return 0; // indicates successful termination
} // end function main