adding rows

This is a discussion on adding rows within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that reads a text file of which the first line contains the number ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    adding rows

    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
    Semper Fi!

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    On quick inspection i noted this:

    Quote Originally Posted by RedZippo

    Code:
     double aveGrade (int aTableA[], int cols)
     {
    	 double gradeSum = 0;
    	 for (int i=0; i < tableExams; i++)
    		 gradeSum = gradeSum + aTableA[i]; 
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  2. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  5. Adding Columns and Rows..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21