By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

inputing data into 2d array and displaying

P: 12
Have a program that is able to take the numbers out of the .txt if it is just a list of numbers. Problem is, I have a file with Names before the grades. I believe i need to set up another array inside the first one to fill with names. The .txt file will look like:

Name
grade
grade
grade
grade
grade
grade
grade
grade
grade
grade
Name
...

Have 15 Names with 10 grades under each.

Here is what i have now:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

const int TESTS = 10;
const int STUDENTS = 15;

void displayGrades( int [][TESTS] );
void getGrades( int [][TESTS]);

int main()
{
int data[STUDENTS][TESTS] = {0};

getGrades( data );

cout <<fixed <<setprecision(1);

displayGrades( data);

#ifdef WIN32
system("pause");
#endif
return 0;
}

void getGrades(int data[] [TESTS])
{
ifstream inputFile;
inputFile.open("GradeSpring07.txt");
if (!inputFile)
{

cout << "Error opening file!" << endl;
exit( 1 );
}





for (int student=0; student< STUDENTS ; student++)
{
for (int test=0; test<TESTS;test++)
{


inputFile >> data[student][test];





}

}
}
void displayGrades( int data[][TESTS] )
{

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];

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++)
{
int grade = data[student][test];
cout << right <<setw(5) << grade <<" ";


classTotal += grade;
}

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;
}
Oct 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: 12
anyone have a suggestion?
Oct 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.