I am trying to write a program that reads a text file of which the first line contains the number of rows and columns; and then find the average for each row. I can do it if the array is a fixed size, but I am having trouble writing a program that will work if the array size changes. So I thought the best way to do it would be to read in each row as a one-dimensional array. I was able to do that with a simpler program that didn't use double vectors.
This program gives me two errors, one for aveGrade "cannot convert parameter" (commented in the code) and one for tableA[i] "illegal use of this type as an expression". I must be passing the wrong arguments to the function, but I am lost as to what to do about it. Can someone help me get this function...functional?
THANKS!
Code:
/*reads a data file containing student scores on several exams.
* The grades for each student are averaged and displayed, and the average score for
* each exam is calculated and displayed. */
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "gradeArray.h"
using namespace std;
int tableGrades, tableExams;
int main()
{
const string INPUT_FILE = "grades.txt";
cout << "This program will calculate the average grade for each student\n";
vector<TableRow>TableA;
fill(INPUT_FILE, TableA);
aveGrade(TableA[tableGrades], tableExams); // this doesn't work --
// generates a "cannot convert parameter..."
for (int row = 0; row < tableGrades; row++)
cout << endl << "Average Grade for Student number "
<< row << " = " << aveGrade(TableA[row], tableExams);
}
/* fill reads the data file and fills the array */
void fill(const string& fileName, Table& aTable)
{
ifstream in(fileName.data() );
assert(in.is_open() );
int rows, columns;
in >> rows >> columns;
cout << "Rows = " << rows << "\n"
<< "Columns = " << columns << "\n";
tableGrades = rows;
tableExams = columns;
Table locTable(rows, TableRow(columns));
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
in >> locTable[r][c];
aTable = locTable;
}
double aveGrade (int aTableA[], int cols)
{
double gradeSum = 0;
for (int i=0; i < tableExams; i++)
gradeSum = gradeSum + TableA[i]; // "illegal use of this type as an expression"
return gradeSum / tableExams;
}
This is the header file:
Code:
/*contains the declarations for type Table */
#include <vector>
#include <iostream>
#include <string>
using namespace std;
typedef vector<double>TableRow;
typedef vector<TableRow> Table;
// fill a table with values from a file
// whose first line is the # of rows and columns
void fill(const string& fileName, Table& aTable);
// aveGrade determines the average grade for a given student
// finds the average values in a one-dimensional array
double aveGrade (int aTableA[], int cols);
Here is a sample data file:
Code:
3 3
95 40 85
30 95 100
85 90 55