I need to write a program for class that calculates student averages and quiz averagages. It has to read a 2-dim array from a file that looks like this:
10 10 10
2 0 1
8 6 9
8 4 10
The four students are the rows and the three quizzes are the columns.
Then I have to change the 2-dim inout into 1-dim arrays to get this output.
My output needs to look like this:
Student Averages Quizzes
1 10.0 10 10 10
2 1.0 2 0 1
3 7.7 8 6 9
4 7.3 8 4 10
Quiz Averages = 7.0 5.0 7.5

So, this is what I have so far. I used the prototypes my insturctor gave us, but I don't know if I am calling them correctly in the main. Then after the main. I am processing the gerMatrix, compStuAvg, and compQuizAvg and I need to display my results using my displayData, but here I get epically lost. Can I get a push in the right direction? Do I need a loop in the main also to process my data? I feel quite lost. Thanks to anyone who takes a look. And don't be mean! I'm a beginner! :)

/* Description: To manipulate one and two dimensional arrays and pass arrays as
 * arguments to functions in order to calcualte student averages and quiz 
 * averages.
 */
 
 # include <fstream>
 # include <iostream>
 
 using namespace std;
 
 const int NUM_STU = 4;
 const int NUM_QUIZ = 3;
  
 // function prototypes
 void getMatrix (ifstream&, int matrix [] [NUM_QUIZ]);
 //reads matrix from file
 void compStuAvg (const int matrix [] [NUM_QUIZ], float []); 
 //calcs avgs to a one-dimensional array
 void compQuizAvg (const int matrix [] [NUM_QUIZ], float []); 
 //calcs quiz avgs
 void displayData (ofstream& outfile, const int matrix [] [NUM_QUIZ], const float [], 
 const float []);
 //displays data
 
 int main ()
 {
     ifstream data;
     ofstream out;
     int matrix [NUM_STU][NUM_QUIZ]; 
     float student_avg, quiz_avg;
     
             
     
     data.open ("grade_matrix.txt"); //file for input
     if (!data)
        {
               cout << "Error!!! Failure to open grade_matrix.txt" << endl;
               system ("pause");
               return 1;
        }
     out.open ("out.txt"); //file for output
     if (!out)
        {
               cout << "Error!!! Failure to open out.txt" << endl;
               system ("pause");
               return 1;
        }
        
               
        getMatrix (data, matrix);
        compStuAvg (matrix, student_avg);
        compQuizAvg (matrix, quiz_avg);
        displayData (outfile, matrix, student_avg, quiz_avg); 
        
        //do I need a loop here??? Such as a while (data)... {}
        
        
        
     system ("pause");
     return 0;
     
 } //end of main
 
 void getMatrix (ifstream& data, int matrix [] [NUM_QUIZ])
 {
           
      for (int i = 0; i < NUM_STU; i++)
      {
                  
          for (int j = 0; j < NUM_QUIZ; j++)
          {
            data >> matrix [i][j];
          }//inner loop
      }//outer loop
      
 }//getMatrix
 
 void compStuAvg (const int matrix [] [NUM_QUIZ], float student_avg [])
 {
      float sum; 
      for (int i = 0; i < NUM_STU; i++)
      {
          sum = 0;
          for (int j = 0; j < NUM_QUIZ; j++)
              sum = sum + matrix [i][j]; 
          student_avg [i] = sum / NUM_STU;    
          //should I be returning a value or even just 0???
           
          
      }
 }
 //compStuAvg
 
 void compQuizAvg (const int matrix [] [NUM_QUIZ], float quiz_avg []);
 {
      float sum;
      for (int i = 0; i < NUM_STU; i++)
      {
          sum = 0; 
          for (int j = 0; j < NUM_QUIZ; j++)
              sum = sum + matrix [i][j];
          quiz_avg [i] = sum / NUM_QUIZ;
            //should I be returning a value or even just 0???
          
      }
 }
 //compQuizAvg
 
 void displayData (ofstream& outfile, const int matrix[] [NUM_QUIZ], 
 const float student_avg[], const float quiz_avg[]) 
 outfile <<
 /*??? I'm lost here after I do the above calculations, how do I 
 display that??? I can't find a good example anywhere quite like this in my book
 or on the web...//
 
 
 

 
 
 
        
              
 




































/

In DisplayData() you need a loop for(int i = 0; i < NUM_STU; i++) . Then within that loop diaply the loop counter followed by each array value

cout << i+1 << " " << student_avg[i] << " ";
for(int j = 0; j < NUM_QUIZ; j++)
   cout << matrix[i][j] << " ";
cout << '\n'; // move cursor to next line

Wow, thanks. I kinda feel dumb! Its what I was thinking-ish, but for some reason, I overthought it as usual, and started screwing up. Haha. I'll see what happens from here. Again, thanks!

This article has been dead for over six months. Start a new discussion instead.