0

Ok, so my program complies (not that that means much, haha), but at least I can now SEE that something in my loops isn't right. I need to take my 2-dim array file input of 4 students with 3 quizzes, which looks like this: (the asterisks are whitespace)
10**10**10
*2***0***1
*8***6***9
*8***4**10

And I need my output to look like this: (asterisks are still whitespace)
Student***Average***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 Average = *****7.0*5.0*7.5

So...what I have so far prints my original matrix and it does some bizarre average and it doesn't print in any semblance of that. I followed the examples I could find, but maybe someone could give me a better example, or at least maybe tell me where my loop is screwed up??? I know it is... THANK YOU ALL, AS ALWAYS!!!

/* Program: Grade Matrix
 * 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 (ofstream& outfile, const int matrix [] [NUM_QUIZ], float []); 
 //calcs avgs to a one-dimensional array
 void compQuizAvg (ofstream& outfile, 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 [4]; // row index of 4 students
     float quiz_avg [3]; // column index of 3 quizzes
     
             
     
     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;
        }
        out << "STUDENT   " << "AVERAGE   " << "QUIZZES" << endl;
               
        getMatrix (data, matrix);
        compStuAvg (out, matrix, student_avg);
        compQuizAvg (out, matrix, quiz_avg);
        displayData (out, matrix, student_avg, quiz_avg); 
        
        
        
     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 (ofstream& out, 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;   
          out << student_avg [i] << " " ; 
      }
      out << endl << endl;
 } //compStuAvg
 
 void compQuizAvg (ofstream& out, const int matrix [] [NUM_QUIZ], float quiz_avg [])
 {
      float sum;
      
      for (int j = 0; j < NUM_QUIZ; j++)
      {
          sum = 0; 
          for (int i = 0; i < NUM_STU; i++)
              sum = sum + matrix [i][j];
          quiz_avg [j] = sum / NUM_QUIZ;
          out << quiz_avg [j] << " " ;
      }
      out << endl << endl;
 }
 //compQuizAvg
 
 void displayData (ofstream& out, const int matrix[] [NUM_QUIZ], 
 const float student_avg[], const float quiz_avg[]) 
 {
  
       for(int i = 0; i < NUM_STU; i++)
       {               
           for(int j = 0; j < NUM_QUIZ; j++)   
               out << matrix[i][j] << " ";
           out << endl; 
       }
       out << endl << endl; //
 }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by WaltP
0

First of all, when asking a question, as always, explain exactly what the program does wrong. It's nice and extremely helpful to know the input and expected output as you posted, but "it doesn't print in any semblance of that" is not descriptive enough. For all we know, the output is:

Mary had a barely queen
it's 3.14159 inches in burblex

which shows something very wrong. Whereas if the output is

StudentAverageQuizzes110.010101021.020137.786947.38410QuizAverage=7.05.07.5

we know exactly what we're looking for.

As it is, I suspect you need to use the iomanip header file and some of the stuff in there like setw and it's brothers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.