So I have calculated the total of students on all of their 6 grades but now I want to be able to calculate the average of all the grades and list the averages next to the total column.

Any help would be greatly appreciated!

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn();

ifstream fin;

//function definitions
int main(){
    system("color f0");
    headerfn(); //function call
        
    fin.open("source.txt");
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
       
     int scores[7][6];
       
    //read the data
    for(int row=0;row<7;row++){
        for(int col=0;col<6;col++)
        fin>>scores[row][col];
        }//end of outer for
  
    int rowtotals[7];
    int coltotals[6];
    
    for(int row=0;row<7;row++){
    rowtotals[row]=0;
    for(int col=0;col<6;col++)
    rowtotals[row]=rowtotals[row]+scores[row][col];
    }//end of outer for

    for(int col=0;col<6;col++){
            coltotals[col]=0;
            for(int row=0;row<7;row++)
            coltotals[col]+= scores[row][col]; 
            }//end of outer for
            
    for(int row=0;row<7;row++){
    cout<<setw(5)<<rowtotals[row]<<endl;
    }//end of outer for 
/*    
                         
    system("pause");
    return 0;
}//end of main

//***********************************************
//void function without parameters
void headerfn(){
     cout<<"******************************************"<<endl;
     cout<<"*Arrays calculation*"<<endl;
     cout<<"******************************************"<<endl;
     }//end of headerfn

Source text file (source.txt) can contain some random data:
54 34 65 34 43 23
76 54 34 54 23 87
87 67 45 65 87 67
45 76 87 65 45 65
34 65 76 87 56 87
76 56 78 98 78 67
87 67 76 87 78 67

Are you having trouble with the calculation or the output? You already have the row and column totals, just divide by the total number (you'll have to cast the numerator (or both numerator and denominator) to double -- like rowaverage[i]=((double)rowtotal[i])/7; For the output it will be just like the one in the other program, for loop over the rows{ output names, for loop over scores, output average for the row} next row, etc, then on the final row print the column averages.

So I have calculated the total of students on all of their 6 grades but now I want to be able to calculate the average of all the grades and list the averages next to the total column.

Any help would be greatly appreciated!

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn();

ifstream fin;

//function definitions
int main(){
    system("color f0");
    headerfn(); //function call
        
    fin.open("source.txt");
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
       
     int scores[7][6];
       
    //read the data
    for(int row=0;row<7;row++){
        for(int col=0;col<6;col++)
        fin>>scores[row][col];
        }//end of outer for
  
    int rowtotals[7];
    int coltotals[6];
    
    for(int row=0;row<7;row++){
    rowtotals[row]=0;
    for(int col=0;col<6;col++)
    rowtotals[row]=rowtotals[row]+scores[row][col];
    }//end of outer for

    for(int col=0;col<6;col++){
            coltotals[col]=0;
            for(int row=0;row<7;row++)
            coltotals[col]+= scores[row][col]; 
            }//end of outer for
            
    for(int row=0;row<7;row++){
    cout<<setw(5)<<rowtotals[row]<<endl;
    }//end of outer for 
/*    
                         
    system("pause");
    return 0;
}//end of main

//***********************************************
//void function without parameters
void headerfn(){
     cout<<"******************************************"<<endl;
     cout<<"*Arrays calculation*"<<endl;
     cout<<"******************************************"<<endl;
     }//end of headerfn

you allready have an array of coltotals with i think a size of 6.

i goes from 0 to < 6

colaverages = coltotals/6;

Mike

I am having problem with both calculation and output. I entered the following code but getting an error, "Invalid types float(int) for array subscript"

float colavg;
    for(int col=0;col<6;col++){
        colavg[col]=((float)coltotals[col])/6;
        }
        
    for(int col=0;col<6;col++){
    cout<<setw(5)<<colavg[row]<<endl;
    }

float colavg[b][6][/b]; Also in your second for loop you wrote row for the index and I think you meant col

I was actually just going to post saying I realized my mistake. But you are right on the money! :) Also, I think under the cout line I need to change [row] to [col]

Now I am trying to add up the first 3 columns (programs) and average them out. I don't need to output the program totals but only the average of them. I am getting the error: "name lookup for row changed for new ISO for scoping

//program total
    int progtotals[3];
    for(int col=0;col<3;col++)
    progtotals[col]=progtotals[col]+scores[row][col];
    }//end of outer for
    
//program average    
    float progavg[3];
    for(int col=0;col<3;col++){
        progavg[col]=((float)progtotals[col])/3;
        }    
    for(int col=0;col<3;col++){
    cout<<setw(5)<<progavg[col]<<endl;
    }

It's complaining about line 4 -- if you used row as the loop variable in another for, by the time it exits (you close the brace or you have a single statement for) the variable disappears. Either put a counter to keep track of rows (called rowctr or something) along with the for loop or somehow encompass you column calculation within another larger for loop which steps over rows.

I am getting incorrect amount. I made some changes to the code

//program total
    int progtotals[3];
    for(int row=0;row<7;row++){
    progtotals[row]=0;
    for(int col=0;col<3;col++)
    progtotals[col]=progtotals[col]+scores[row][col];
    }//end of outer for
    
//program average    
    float progavg[3];
    for(int col=0;col<3;col++){
        progavg[col]=((float)progtotals[col])/3;
        }    
    for(int col=0;col<3;col++){
    cout<<setw(5)<<progavg[col]<<endl;
    }

Swap lines 3 and 5, so you're summing over the rows first and change line 4 to col instead of row. Draw a quick diagram on a piece of paper to see the proper order.

I am not trying to sum up the rows though. For say.. these are the grades of the students listed from top to the bottom. I want first 3 grades for program avg for the first student, last 3 grades for test avg and then go to the next row and get first 3 program grades avg for that student, last 3 grades for test avg and so on..
I guess it would make it easier if I had names listed on the left but I can't figure out a way to display them.
This is how they're supposed to be like.
fname lname 23 43 54 23 43 54 (first 3 grades for prog avg, last 3 grades for test avg)
fname lname 13 45 43 23 23 43

double progave[7], testave[7], progsum[7], testsum[7];
for (int row = 0;row<7;row++)
{ 
        for (int col = 0;col<3;col++)
            progsum[row] += scores[row][col];
        progave[row] = (double)progsum[row]/3;

        (now you can fill in the next one)
}

You had the code for the display method in the other thread...

Edited 6 Years Ago by jonsca: n/a

Thanks Jon. Only problem is I was getting three values for the same thing and the value for very last student and the third one from the last are incorrect.

float progavg[7], testavg[7], progsum[7], testsum[7];
for (int row = 0;row<7;row++)
{ 
        for (int col = 0;col<3;col++)
        progsum[row] += scores[row][col];
        progavg[row] = (float)progsum[row]/3;
        for (int col = 0;col<3;col++){
        cout<<setw(5)<<progavg[row]
        <<endl;
        }

In order to prevent the same number from coming three times, I changed col<3 to col<1 and the numbers are showing once like how they should even though I am not sure about the logic being correct. The values for very last student and third one from the last are still incorrect. I really do appreciate your help looking into this.

I had meant for you to write out the code for the testavg portion of this for loop. Don't display anything yet. Look at the second for loop. How does the row value change over those 3 column values? it doesn't.

Edited 6 Years Ago by jonsca: n/a

Getting closer!
The progavg for the very last and the third one from last is still coming garbage. The fourth student's test avg is also inaccurate.

float progavg[7], testavg[7], progsum[7], testsum[7];
for (int row = 0;row<7;row++)
{ 
        for(int col = 0;col<3;col++)
            progsum[row] += scores[row][col];
        progavg[row] = (float)progsum[row]/3;
        for(int col = 3;col<6;col++)
            testsum[row] += scores[row][col];
        testavg[row] = (float)testsum[row]/3;
}

cout<<left<<fixed<<showpoint<<setprecision(2);
for (int row = 0;row<7;row++)
    cout<<setw(5)<<progavg[row]<<endl;
for (int row = 0;row<7;row++)
    cout<<setw(5)<<testavg[row]<<endl;

Nice! How much does the calculated average deviate from the hand calculation?

EDIT: I see what you mean something is undefined somewhere

Edited 6 Years Ago by jonsca: n/a

it reads like
186860081461547200000000000000000000000.00
then the one below it shows up just fine..
and then similar garbage
three below it for test avg shows fine..
then garbage
and ones below are fine.

Could it be because the total, cavg, totalavg and testavg is aligned vertically.. when they should be in aligned column by column?
I also realized that I totally made mistake on courseavg. It should be changed to (progavg+testavg)/2 and not scores/6

Do this to initialize your arrays: float progavg[7]={0}, testavg[7]={0}, progsum[7]={0}, testsum[7]={0};

Yes!! Sir, you are a genius! :)
Would you be able to kindly help me sort this out? rightnow.. i have all the numbers aligned vertically like under one column... how do I display them so the total is one column, prog avg is second column, test avg is third column and course avg is fourth column
In addition, to getting the names to show up on the very first column.

for loop over your whole set
{
     cout <<firstname[row]  <<" "<<  lastname[row];  
     for (column 1 to 6)
           cout <<scores[row][column]<<" ";
     cout << total[row] <<" "<<progave[row]<<" " <<testavg[row] 
               <<courseavg[row];
    cout <<endl;     //put all your setw type stuff in place of the " "as 
                              //needed
}

since if you don't put a endl in there, it all comes out on the same line

Edited 6 Years Ago by jonsca: n/a

Where exactly does that part go on the code? I was not sure on how to get the names to show up. If you don't mind checking my arrays for names that I currently have commented. As far as re-arranging to columns, if I am not mistaken, instead of putting "endl", I need to put " "?

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn(); //void function without parameters

ifstream fin;

//function definitions
int main(){
    system("color f0");
    headerfn(); //function call
        
    fin.open("input4.txt");
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
    
//     string fnames[7][1],lnames[7][1];       
     int scores[7][6];
     
    //read the data

/*
    for(int row=0;row<7;row++){
        for(int col=0;col<2;col++)
        fin>>fnames[row][col]>>lnames[row][col];
        }//end of outer for
*/            
    //read the data
    for(int row=0;row<7;row++){
        for(int col=0;col<6;col++)
        fin>>scores[row][col];
        }//end of outer for

//For exam, counter is not going to be used.

 //displays the numbers only
/*

   for(int row=0;row<7;row++){
           for(int col=0;col<2;col++)
           cout<<setw(5)<<names[row][col];
           cout<<endl;
           }//end of outer for
    cout<<endl;
     
    for(int row=0;row<7;row++){
           for(int col=0;col<2;col++)
           cout<<setw(5)<<fnames[row][col]<<lnames[row][col];
           cout<<endl;
           }//end of outer for
    cout<<endl;
   
*/      
    int rowtotals[7];
    int coltotals[6];
    
    for(int row=0;row<7;row++){
    rowtotals[row]=0;
    for(int col=0;col<6;col++)
    rowtotals[row]=rowtotals[row]+scores[row][col];
    }//end of outer for

    for(int col=0;col<6;col++){
            coltotals[col]=0;
            for(int row=0;row<7;row++)
            coltotals[col]+= scores[row][col]; //+= is same as saying coltotals[col]+
            }//end of outer for
            
    cout<<left<<fixed<<showpoint<<setprecision(2);
   
    for(int row=0;row<7;row++){
    cout<<setw(5)<<rowtotals[row]<<endl;
    }//end of outer for 

//course average    
    float colavg[6];
    for(int col=0;col<6;col++){
        colavg[col]=((float)coltotals[col])/6;
        }
        
    for(int col=0;col<6;col++){
    cout<<setw(5)<<colavg[col]<<endl;
    }

float progavg[7]={0}, testavg[7]={0}, progsum[7]={0}, testsum[7]={0}; 
for (int row = 0;row<7;row++)
{ 
        for(int col = 0;col<3;col++)
            progsum[row] += scores[row][col];
        progavg[row] = (float)progsum[row]/3;
        for(int col = 3;col<6;col++)
            testsum[row] += scores[row][col];
        testavg[row] = (float)testsum[row]/3;
}

cout<<left<<fixed<<showpoint<<setprecision(2);
for (int row = 0;row<7;row++)
    cout<<setw(5)<<progavg[row]<<endl;
for (int row = 0;row<7;row++)
    cout<<setw(5)<<testavg[row]<<endl;

            
    
/*    
    //string scores[7];
//==    float prog[7][3], test[7][3];
    
    //hold the row
    for(int row=0;row<7;row++){
           // fin>>scores[row];
            for(int col=0;col<3;col++)
            fin>>prog[row][col]; //took care of 3 program values
            for(int col=0;col<3;col++)
            fin>>test[row][col];
}//end of outer for

    
    cout<<left<<fixed<<showpoint<<setprecision(2);
       
       float progavg[3],testavg[3];
       for(int row=0;row<12;row++){
               progavg[row]=0;
               testavg[row]=0;
               for(int col=0;col<3;col++){
                       progavg[row]+=(prog[row][col])/3.0;
                       testavg[row]+=(test[row][col])/3.0;
                       }//inner for loop
                       }//outer for loop
                        
                       for(int row=0;row<7;row++)
                       cout<<progavg[row]<<testavg[row];
/*       
//       cout<<"Program Average"<<endl;
       for(int row=0;row<7;row++){
//               cout<<setw(5)<<scores[row];
       for(int col=0;col<3;col++)
               cout<<setw(5)<<prog[row][col];
               cout<<progavg[row]<<endl; //modify later
}//end of outer for
               
//               cout<<"Test Average"<<endl;
               for(int row=0;row<7;row++){
//                       cout<<setw(5)<<scores[row];
                       for(int col=0;col<4;col++)
                       cout<<setw(5)<<test[row][col];
                       cout<<testavg[row]<<endl; //modify later
}     
*/
                         
    system("pause");
    return 0;
}//end of main

//***********************************************
//void function without parameters
void headerfn(){
     cout<<"******************************************"<<endl;
     cout<<"******Arrays******"endl;
     cout<<"******************************************"<<endl;
     }//end of headerfn

Edited 6 Years Ago by amishraa: forgot to include CODE

I think the most logical place to put it is right at the very end. If you needed to do it where you print your header you'll have to pass them all in but if you don't need to do it, don't bother.

what is wrong with the way I have my names declared? say the first two columns are for names (first and last name)

string fnames[7][1],lnames[7][1];
for(int row=0;row<7;row++){
        for(int col=0;col<2;col++)
        fin>>fnames[row][col]>>lnames[row][col];
        }//end of outer for

Because your input file has your names, then the scores for the name. What you have would work if the file only had names in it. Go back and grab your other post as you had the input down quite well in that one. I've gotta be off for a while, but I'll check back later. You've done a great job so far and you're really close to the end (unless your program has some hidden features you're not revealing hehe).

Comments
Very helpful and patience enough to analyze and work through problems. He deserves a gold medal! :)

In addition, I am trying to get the course avg = (progavg + testavg)/2 but the results are over 200 points when they should be the average of those two scores.

//course average    
    float courseavg[2];
    for(int col=0;col<6;col++){
        courseavg[col]=(progsum[col]+testsum[col])/2;
        }
        
    for(int col=0;col<6;col++){
    cout<<setw(5)<<courseavg[col]<<endl;
    }

Jon, Thank you. I could not have accomplished anything without your time and patience. I really appreciate you for helping me through all this! I feel much.. much better than from where I had started this morning. :)
The way the final program is supposed to look like:
fname lname progavg testavg courseavg letter grade
fname1 lname1 progavg1 testavg1 courseavg1 letter grade1
fname2 lname2 progavg2 testavg2 courseavg2 letter grade2

Currently I have:
progavg1
progavg2
progavg3
testavg1
testavg2
testavg3
courseavg1 (though, these are incorrect)
courseavg2
courseavg3

Like you suggested, I am going to use the other input file and see where I can go with them. I'll look forward to hear back from you later too. :)

nevermind the post in regards to avg coming up incorrect, I realized the silly mistake! I added sums of total and test when they should have been avg of total and test :P

Edited 6 Years Ago by amishraa: n/a

okay.... so here is a bit of a progress.
I got it fixed so the prog total, prog avg, test avg, course avg are all aligned side by side.
However, I am working from input file that has no names.
If I use the input file used on the other thread, everything gets jumbled up.
I need to find a way to use the input file with name and then display the names on first column next to total scores.
Here is the most recent source code:

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn(); //void function without parameters

ifstream fin;
ifstream fout;

//function definitions
int main(){
    system("color f0");
    headerfn(); //function call
        
    fin.open("scores.txt");
    fout.open("results.txt");
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
    if(!fout){
              cout<<"Output Failure"<<endl;
              system("pause");
              return 1;
              } //end of error check
    
     //string fnames[7][1],lnames[7][1];       
     int scores[7][6];
           
    //read the data
    for(int row=0;row<7;row++){
        for(int col=0;col<6;col++)
        fin>>scores[row][col];
        }//end of outer for
   
      
    int rowtotals[7];
    int coltotals[6];
    
    for(int row=0;row<7;row++){
    rowtotals[row]=0;
    for(int col=0;col<6;col++)
    rowtotals[row]=rowtotals[row]+scores[row][col];
    }//end of outer for

    for(int col=0;col<6;col++){
            coltotals[col]=0;
            for(int row=0;row<7;row++)
            coltotals[col]+= scores[row][col]; //+= is same as saying coltotals[col]+
            }//end of outer for

//calculates the programs sum and tests sum
float progavg[7]={0}, testavg[7]={0}, progsum[7]={0}, testsum[7]={0}; 
for (int row = 0;row<7;row++)
{ 
        for(int col = 0;col<3;col++)
            progsum[row] += scores[row][col];
        progavg[row] = (float)progsum[row]/3;
        for(int col = 3;col<6;col++)
            testsum[row] += scores[row][col];
        testavg[row] = (float)testsum[row]/3;
}
    
//calculates the course average    
    float courseavg[2];
    for(int col=0;col<7;col++){
        courseavg[col]=(progavg[col]+testavg[col])/2;
        }

/*    <=== Can't figure out how to do if/else statement on arrays
    char grade[7];
    for(int row=0;row<7;row++){
    if (courseavg[col] >=90 )grade[row]='A';
    else if (courseavg >=80 )grade[row]='B';
    else grade='F';
}
*/

    cout<<fixed<<showpoint<<setprecision(2);
    for(int row=0;row<7;row++)
    cout<<setw(10)<<rowtotals[row]<<setw(10)<<progavg[row]<<setw(10)<<testavg[row]<<setw(10)<<courseavg[row]<<endl;
                         
    system("pause");
    return 0;
}//end of main

//***********************************************
//void function without parameters
void headerfn(){
    cout<<"**********************************************************************\n"<<endl;
	cout<<"        Students grades using Arrays\n                          "<<endl;
	cout<<"        Programmer:  James Bond                                    \n"<<endl;
    cout<<"        Program: Arrays                                                \n"<<endl;
	cout<<"        This program uses function to read student information from\n";
	cout<<"        an input text file into string and integer arrays.\n";
	cout<<"        It then calculates and output averages and\n";
	cout<<"        grades to the monitor as well as to an output text file\n"       <<endl;
    cout<<"**********************************************************************\n";
    cout<<endl;
    
    cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;          
    cout<<left<<setw(20)<<"Student Name"<<setw(10)<<"Total"<<setw(10)<<"Program"<<setw(10)<<"Test"<<setw(10)<<"Course"<<setw(10)<<"Grade"<<endl;
    cout<<right<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average"<<setw(10)<<"Average"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;
     }//end of headerfn

Instead of using the input with only numbers of 7 rows and 6 columns, I would like to use input file that has all of the students' names on the first column followed by 7 columns of the scores.

Edited 6 Years Ago by amishraa: n/a

This question has already been answered. Start a new discussion instead.