I have to write a program that will read a file that lists the scores for two teams.

1st 2nd 3rd 4th
Visitors 17 18 21 20 Final Score: 76
Home 21 22 24 16 Final Score: 83

Input file will look like: 17 18 21 20 21 22 24 16


The program must have a function for each of the following:

1) readFile - read from the file and populate the 2d array
2) finalScore - prints 'Visitors or Home", each qtr. score and final score
3) averageQtrScore - prints both teams' average quarter score
4) highScore - print the highest score and which quarter

Sample Output:

FinalScore
Visitors 17 18 21 20 76
Home 21 22 24 16 83

Visitors Average Quarter Score: 20.871 (made up answer)
Home Average Quarter Score: 21.234 (made up answer)

Home team had highest scoring quarter. They had 24 points in quarter #3.

I have to get the program to look like that, but not exactly like that. So far, I'm having trouble getting the program to run at all. I appreciate the help. I'm trying to figure out one error saying "declaration of '<name>' as array of references.


Here is my code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 5;


void readFile(ifstream& inData, ofstream& outData);
void finalScore(int& totalscores[][cols]);
void averageQtrScore (int& team_avg_scores[][cols]);
void highscore (int& highestscore[][cols], int totalscores);


int main()
{
    
    int Homescores[4] = {'22','23','25','17'};
    int Visitorscores[4] = {'18','19','22','21'};
    string teams[2] = {"Visitors","Home"};
    int avg;
    int high_score;
    
    ifstream inData;
    ofstream outData;
    
    outData<<fixed<<showpoint;
    outData.precision(2);
    
    readFile(inData,outData);
    finalScore(Homescores,Visitorscores);
    averageQtrScore(avg);
    highscore(high_score,Homescores,Visitorscores);
    
    inData.close();
    outData.close();
    
    system ("PAUSE");
    return 0;
    
    void readFile (ifstream& inData, ofstream& outData);
    {
         inData.open("UnknownLab.txt")
         outData.open("UnknownLab_output.txt");
    }
    
    void finalScore(int& totalscores);
    {
       while (inData>>scores)
       {
             int sum = 0;
             
             sum+=scores;
             
             cout<<teams<<" "<<scores<<endl;
         
   
    }
    
    void averageQtrScore (int& team_avg_scores);
    {
         
         int j=1;
         int i=1;
         
         for (i=0; i<rows; i++)
         {
             for (int j=0; j<cols; j++)
             
             sum+=grades[i][j];
             
             avg = sum/cols;
             
             cout<<"Vistors average score is "<<(i+1<<" is "<<avg<<endl;
             sum = 0; 
         
    }
    
    void highscore (int& highestscore, int totalscores);
    {
         while (inData>>scores)
         {
               int scores;
               
               highestscore = high_score[0];
               
               for (int i = 0; i<cols; i++)
               
               if (scores < highest)
               highest = high_score[i];
               cout<<
         
    }

First off, decide on your data structure(s) and stick with that. You have two 1D arrays for the scores declared, but have a three of your functions set up for 2D arrays.

In those functions, your declaration of the the array parameter is probably what's throwing the error you mention (please give line numbers that errors point to).

void highscore( int scores[][cols], .....

Omit the ampersand in the declaration of the array.

Look again at what your function descriptions (and names) are. Does your readfile( ) actually read anything? Does it have any business at all dealing with the output?

Does finalscore( ) have any reason to read in from the input? And how does it have access to the inData file handle?

Attack a project like this a piece at a time. Get one part working, then move to the next. For this, you need to read in data, after having opened the file (and checked that it successfully opened.) Then do the finalScore( ) function, so you can verify you've read the data in correctly, and passed the array around successfully.

Get that done, repost where you are and ask further questions.

I tried to follow what you told me...I still couldn't get it to run, but I changed a lot of material to make sure things matched up etc.

I am getting errors like:

33 cannot convert int*' toint ()[5]' for argument 1' toint finalScore(int ()[5], int (*)[5])'

note C:\Dev-Cpp\include\c++\3.4.2\bits\istream.tcc:87 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

Here is my updated code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 5;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int Hscores[][cols], int Vscores[][cols]);
int averageQtrScore (int team_avg_scores[][cols], int Hscores[][cols], int Vscores[][cols]);
int highscore (int highestscore[][cols], int Hscores[][cols], int Vscores[][cols]);


int main()
{

    int Homescores[4] = {22,23,25,17};    
    int Visitorscores[4] = {18,19,22,21};
    string teams[2] = {"Visitors","Home"};
    int avg;
    int high_score;

    ifstream inData;
    ofstream outData;

    outData<<fixed<<showpoint;
    outData.precision(2);

    readFile(inData,outData);
    finalScore(Homescores,Visitorscores);
    averageQtrScore(avg,Homescores,Visitorscores);
    highscore(high_score,Homescores,Visitorscores);

    inData>>Homescores>>Visitorscores;
    outData>>Homescores>>Visitorscores<<endl;

    inData.close();
    outData.close();

    system ("PAUSE");
    return 0;

    void readFile (ifstream& inData, ofstream& outData);
    {
         inData.open("Unknown.txt");
         outData.open("Unknown_output.txt");
    }

    int finalScore(int Hscores[][cols],int Vscores[][cols]);
    {
       while (inData>>Homescores>>Visitorscores)
       {
             cout<<teams<<" "<<Homescores>>Visitorscores<<endl;
       }

    }

    int averageQtrScore (int team_avg_scores[][cols],int Hscores[][cols],int Vscores[][cols]);
    {

         while (inData>>Homescores>>Visitorscores)
        {
         int sum = 0;
         int j=1;
         int i=1;

         for (i=0; i<rows; i++)
          {
             for (int j=0; j<cols; j++)

             sum+=scores[i][j];

             avg = sum/cols;

             cout<<"Vistors average score is "<<(i+1)<<" is "<<avg<<endl;
             sum = 0; 
          }
        }
    }

    int highscore (int highestscore[][cols],int Hscores[][cols],int Vscores[][cols]);
    {
         while (inData>>Homescores>>Visitorscores)
         {
               int scores;
               int highest;
               int highestscore;

               highestscore = high_score[0];

               for (int i = 0; i<cols; i++)

               if (scores < highest)
               highest = high_score[i];

         }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Like I said, get your data straightened out first. Here's a snippet of your code:

const int rows = 2;
const int cols = 5;

int finalScore(int Hscores[][cols], int Vscores[][cols);

int main()
{
    
    int Homescores[4] = {22,23,25,17};    
    int Visitorscores[4] = {18,19,22,21};
    
    finalScore(Homescores,Visitorscores);

finalscore( ) is taking two 2D arrays, of column dimension 5. You are passing to it two 1D arrays of dimension 4. That's apples and tomatoes.

Your problem statement calls for your data to be in a 2D array, so do that. Declare an array scores using your row and cols constants. Assume row 0 is Home team, row 1 is Visitors.

While the error message may not seem to make a lot of sense, it is pointing you to this problem of mismatched arguments.

33 cannot convert `int*' to `int (*)[5]' for argument `1' to `int finalScore(int (*)[5], int (*)[5])'

tells you that the compiler doesn't know how to make the int* - which is a way of saying a 1D array of ints - fit as the int*[5] parameter - which is a 2D array of ints, with column dimension 5.

I updated my program. I tried to do what you told me to do. I think I did something right as I'm getting less errors now.

I'm getting this error:

34 initializing argument 1 of `int averageQtrScore(int ()[4], int ()[4])'

Here is my updated code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 4;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int scores[][cols]);
int averageQtrScore (int team_avg_scores[][cols], int scores[][cols]);
int highscore (int highestscore[][cols], int scores[][cols]);


int main()
{

    int teamscores[2][4] = {{22,23,25,17},{18,19,22,21}};    
    string teams[2][2] = {"Visitors","Home"};
    int scores;
    int avg;
    int high_score;

    ifstream inData;
    ofstream outData;

    outData<<fixed<<showpoint;
    outData.precision(2);

    readFile(inData,outData);
    finalScore(teamscores);
    averageQtrScore(avg,teamscores);
    highscore(high_score,teamscores);

    inData>>scores;
    outData<<scores<<endl;

    inData.close();
    outData.close();

    system ("PAUSE");
    return 0;

    void readFile (ifstream& inData, ofstream& outData);
    {
         inData.open("Unknown.txt");
         outData.open("Unknown_output.txt");
    }

    int finalScore(int scores[][cols]);
    {
       while (inData>>scores)
       {
             cout<<teams<<" "<<scores<<endl;
       }

    }

    int averageQtrScore (int team_avg_scores[][cols], int scores[][cols]);
    {

         while (inData>>scores)
        {
         int sum = 0;
         int j=1;
         int i=1;

         for (i=0; i<rows; i++)
          {
             for (int j=0; j<cols; j++)

             sum+=scores[i][j];

             avg = sum/cols;

             cout<<"Vistors average score is "<<(i+1)<<" is "<<avg<<endl;
             sum = 0; 
          }
        }
    }

    int highscore (int highestscore[][cols], int scores[][cols]);
    {
         while (inData>>scores)
         {
               int scores;
               int highest;
               int highestscore;

               highestscore = high_score[0];

               for (int i = 0; i<cols; i++)

               if (scores < highest)
               highest = high_score[i];

         }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

again, look at the function parameters you've set, and what actual argument you're sending when you call the function. Do they match?

Does the function even need two parameters?

averageQtrScore - prints both teams' average quarter score

I take that to mean, pass the scores data to the function, it will compute and display average quarter score (one value per team), and be done.

Remember the KISS principle - keep it simple, student. Your functions should do just exactly what they're required to, and not one line more! Reread your assignment instructions.

I fixed that problem.

Now, I'm trying to figure out a 3 errors that is caused by:

invalid types `int[int]' for array subscript
on
Lines 74, 90, 95

Here is my updated code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 4;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int scores[][cols]);
int averageQtrScore (int scores[][cols]);
int highscore (int scores[][cols]);


int main()
{

    int teamscores[2][4] = {{22,23,25,17},{18,19,22,21}};    
    string teams[2][2] = {"Visitors","Home"};
    int scores;
    int avg;
    int high_score;

    ifstream inData;
    ofstream outData;

    outData<<fixed<<showpoint;
    outData.precision(2);

    readFile(inData,outData);
    finalScore(teamscores);
    averageQtrScore(teamscores);
    highscore(teamscores);

    inData>>scores;
    outData<<scores<<endl;

    inData.close();
    outData.close();

    system ("PAUSE");
    return 0;

    void readFile (ifstream& inData, ofstream& outData);
    {
         inData.open("Unknown.txt");
         outData.open("Unknown_output.txt");
    }

    int finalScore(int scores[][cols]);
    {
       while (inData>>scores)
       {
             cout<<teams<<" "<<scores<<endl;
       }

    }

    int averageQtrScore (int scores[][cols]);
    {

         while (inData>>scores)
        {
         int sum = 0;
         int j=1;
         int i=1;

         for (i=0; i<rows; i++)
          {
             for (int j=0; j<cols; j++)

             sum+=scores[i][j];

             avg = sum/cols;

             cout<<"Vistors average score is "<<(i+1)<<" is "<<avg<<endl;
             sum = 0; 
          }
        }
    }

    int highscore (int scores[][cols]);
    {
         while (inData>>scores)
         {
               int highestscore;

               scores = highestscore[0];

               for (int i = 0; i<cols; i++)

               if (scores < highestscore)
               highestscore = scores[i];

         }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

your missing a '}' in your main. (after return 0; ), but I suspect this is a copy-paste error?

Other problem: averageQtrScore(avg,teamscores); avg is an int and teamscores is a 2d-array of ints. But averageQtrScore expects two 2d-arays.

I don't know what to change it to, because you never use the first argument of the averageQtrScore function

I still need help, i can't figure it out.

In function int finalScore(int (*)[4], std::string (*)[4])': In functionint averageQtrScore(int, int (*)[4])':
InData is undeclared in each function

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 4;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int scores[][cols], string teams[][cols]);
int averageQtrScore (int team_avg_scores, int scores[][cols]);
int highscore (int highestscore, int scores[][cols]);


int main()
{

    int teamscores[2][4] = {{22,23,25,17},{18,19,22,21}};    
    string teams[2][4] = {"Visitors","Home"};
    int scores;
    int avg;
    int high_score;

    ifstream inData;
    ofstream outData;

    outData<<fixed<<showpoint;
    outData.precision(2);

    readFile(inData,outData);
    finalScore(teamscores,teams);
    averageQtrScore(avg,teamscores);
    highscore(high_score,teamscores);

    inData>>scores;
    outData<<scores<<endl;

    inData.close();
    outData.close();

    system ("PAUSE");
    return 0;
}

    void readFile (ifstream& inData, ofstream& outData)
    {
         inData.open("Unknown.txt");
         outData.open("Unknown_output.txt");
    }

    int finalScore(int scores[][cols],string teams[][cols])
    {
       while (inData>>scores)
       {
             cout<<teams<<" "<<scores<<endl;
       }

    }

    int averageQtrScore (int team_avg_scores, int scores[][cols])
    {

         while (inData>>scores)
        {
         int sum = 0;
         int j=1;
         int i=1;

         for (i=0; i<rows; i++)
          {
             for (int j=0; j<cols; j++)

             sum+=scores[i][j];

             team_avg_scores = sum/cols;

             cout<<"Vistors average score is "<<(i+1)<<" is "<<team_avg_scores<<endl;
             sum = 0; 
          }
        }
    }

    int highscore (int highestscore, int scores[][cols])
    {
         while (inData>>scores)
         {
               int scores;
               int highest;
               int highestscore;

               highestscore = scores[0];

               for (int i = 0; i<cols; i++)

               if (scores < highest)
               highest = scores[i];

         }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

in this function you use "indata" which is never declared (in the scope of the function)
:

int finalScore(int scores[][cols],string teams[][cols])
{
    while (inData>>scores)
    {
        cout<<teams<<" "<<scores<<endl;
    }
}

So by changing the function to take in a stream by reference, this problem could be solved. Something like: int finalScore(int scores[][cols],string teams[][cols],ifstream& inData) But then we encounter the next problem: while (inData>>scores) That won't work. you'll have to tell the compiler in which element exactly you want to store the data.

Before I go any further: Have you ever worked with 2d-arrays? Do you understand how they work? If not: you might be better of redoing you program without them. They're not really necessary for your program. Here's a small example to show what I mean:

int ReadAndPrint(string filename)
{
    ifstream in(filename.c_str());
    if (!in.is_open())
        return 0;

    int total=0, number =0;
    for (int j = 0; j < 2; j++) //no of teams
    {
        for (int i = 0; i < 4; i++) //.no of scores
        {
            in >> number;
            cout << number << " ";
            total+=number;
        }
        cout << "total: " << total << "\n";
    }
    return 1;
}

int main()
{
    if (!ReadAndPrint("c:\\input.txt"))
        cout << "Error!";
    return 0;
}

No, this is my first time with 2D arrays. I fixed some problems, and I got the program to run, but no data is printing to my output file. Any reason why?

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;
const int cols = 4;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int scores[][cols], string teams[][cols],ifstream& inData,ofstream& outData);
int averageQtrScore (int team_avg_scores, int scores[][cols],ifstream& inData,ofstream& outData);
int highscore (int highestscore, int scores[][cols],ifstream& inData,ofstream& outData);


int main()
{

    int teamscores[2][4] = {{22,23,25,17},{18,19,22,21}};    
    string teams[2][4] = {"Visitors","Home"};
    int scores;
    int avg;
    int high_score;

    ifstream inData;
    ofstream outData;

    inData>>scores;
    outData<<scores<<endl;

    outData<<fixed<<showpoint;
    outData.precision(2);

    readFile(inData,outData);
    finalScore(teamscores,teams,inData,outData);
    averageQtrScore(avg,teamscores,inData,outData);
    highscore(high_score,teamscores,inData,outData);

    inData.close();
    outData.close();

    system ("PAUSE");
    return 0;
}

    void readFile (ifstream& inData, ofstream& outData)
    {
         inData.open("Unknown.txt");
         outData.open("Unknown_output.txt");
    }

    int finalScore(int scores[][cols],string teams[][cols],ifstream& inData,ofstream& outData)
    {
       while (inData>>scores[2][4])
       {
             outData<<teams<<" "<<scores<<endl;
       }

    }

    int averageQtrScore (int team_avg_scores, int scores[][cols],ifstream& inData,ofstream& outData)
    {

         while (inData>>scores[2][4])
        {
         int sum = 0;
         int j=1;
         int i=1;

         for (i=0; i<rows; i++)
          {
             for (int j=0; j<cols; j++)

             sum+=scores[i][j];

             team_avg_scores = sum/cols;

             outData<<"Vistors average score is "<<(i+1)<<" is "<<team_avg_scores<<endl;
             sum = 0; 
          }
        }
    }

    int highscore (int highestscore, int scores[][cols],ifstream& inData,ofstream& outData)
    {
         while (inData>>scores[2][4])
         {

               highestscore = scores[2][4];

               for (int i = 0; i<cols; i++)

               if (scores[2][4] < highestscore)
               highestscore = scores[2][4];

               outData<<"Home team had the highest scoring quarter. They had "<<highestscore<<" points in quarter #3"<<endl;

         }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

but no data is printing to my output file. Any reason why?

Yes.

This code is the problem:

while (inData>>scores[2][4])
    {
        outData<<teams<<" "<<scores<<endl;
    }

You are writing all the data in element [2][4] of the array and then you try to write the array to a file. This is not possible. You'll have to:
- read in one number
- store that number in an element: scores[x][y] or something(if you want)
- then write that one element back to the output file, so you'll need a few loop/or if statements.

Have a look at my example code, it has some thing in it you could use :)

NL,
You keep hopping all around the program, like trying to put bandaids on someone hit by a bus. Work on this one piece at a time, get it right, then move on to the next.

I'd suggest the following order:
finalScore
averageQtrScore
highScore
readFile

I put readFile last because you can work without that, when you've initialized the data array as you're doing. But when you have the program complete, the declaration of teamscores should set it to all zeros as the initial state.

The teams (team names) array only has to be 1D, as the string type is in essence the 2nd dimension.

As I read the assignment, there is no requirement to store the teams' total scores or averages and pass them back to main( ), they are just displayed from within the functions that derive them.

Only the readFile function has any need for ifstream variable, and, does that variable need to be allocated in main() and passed to it? Couldn't it be allocated, opened, used, and closed in readFile()?

See my comments in the code below.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int rows = 2;  //we usually make constants UPPERCASE, like:
                     // const int ROWS = 2;
const int cols = 4;


void readFile(ifstream& inData, ofstream& outData);
int finalScore(int scores[][cols]);
int averageQtrScore (int team_avg_scores[][cols], int scores[][cols]);
int highscore (int highestscore[][cols], int scores[][cols]);


int main()
{
    
    int teamscores[2][4] = {{22,23,25,17},{18,19,22,21}};  
    //when program complete, this declaration should set array to 0's
    //int teamscores[2][4] = {{0},{0}}; 

    string teams[2][2] = {"Visitors","Home"};

    //these have no role in main( )
    //int scores;
    //int avg;
    //int high_score;
    
    ifstream inData;

    //no requirement to write to a file
    //ofstream outData;
    
    //set formatting for console output, not filestream
    cout << fixed << showpoint;
    cout.precision(2);
    
    readFile(inData,outData);
    finalScore(teamscores);
    averageQtrScore(avg,teamscores);
    highscore(high_score,teamscores);
    
    //huh?
 /*   inData>>scores;
    outData<<scores<<endl;*/
    
    inData.close();
    //outData.close();
    
    system ("PAUSE");
    return 0;
    

    //readFile - read from the file and populate the 2d array
    void readFile (ifstream& inData, /*what else goes here */);
    {
         inData.open("Unknown.txt");

         //test for successful file opening
  
         //read the data, store to array
    }
    

    //finalScore - prints 'Visitors or Home", each qtr. score and final score
    int finalScore(int scores[][cols]);
    {

         //examine each element of array, display, and create totals



    }
    

    //averageQtrScore - prints both teams' average quarter score
    int averageQtrScore (/* what's this? int team_avg_scores[][cols]*/,
                           int scores[][cols]);
    {
        
       //this one is mostly right

         int sum = 0;
         int j=1;
         int i=1;
         
         for (i=0; i<rows; i++)
          {
             //reset sum to 0
             for (int j=0; j<cols; j++)
             
             sum+=scores[i][j];
             
             avg = sum/cols;


             //so only Visitors have an average?
             cout<<"Vistors average score is "<<(i+1)<<" is "<<avg<<endl;
             sum = 0; 
          }
    }
    

    //highScore - print the highest score and which quarter
    int highscore (int highestscore[][cols], int scores[][cols]);
    {
         //while (inData>>scores)   NO file input in this function
         {
               int scores;
               int highest;
               int highestscore;
               
               highestscore = high_score[0]; //three things wrong here!
               
               for (int i = 0; i<cols; i++)  //what about looping rows?
         
               if (scores < highest)  //this loop needs much work
               highest = high_score[i];
              
         }
    }

Thanks for the replys.

I made some adjustments according to what both of you told me to the best of my ability. I'm not completely done with the adjustments though due to not understanding some things. I did most of it though

The program is currently running, but I'm unable to get the scores and final score next to each team. Also, I'm not sure if I'm suppose to do cout<<"Visitor"<<endl; and cout<<"Home"<<endl; to have the team name show up, or am i suppose to run it through a function take care of it? If I do, How do I take care of that?

As of now, I did the team names manually in a cout statement in which the output for "finalScore" function comes out with messed up numbers.

Here is my updated code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int ROWS = 2;
const int COLS = 4;


void readFile(ifstream& inData);
int finalScore(int teamscores[][COLS]);
int averageQtrScore (int teamscores[][COLS]);
int highscore (int teamscores[][COLS]);


int main()
{

    int teamscores[ROWS][COLS] = {{0},{0}};    
    string teams[ROWS] = {"Visitors","Home"};

    ifstream inData;


    cout<<fixed<<showpoint;
    cout.precision(2);

    readFile(inData);
    finalScore(teamscores);
    averageQtrScore(teamscores);
    highscore(teamscores);

    inData.close();

    system ("PAUSE");
    return 0;
}

    void readFile (ifstream& inData)
    {
         inData.open("Lab3.txt");
    }

    int finalScore(int teamscores[][COLS])
    {
        string teams[ROWS] = {"Visitors","Home"};

             cout<<teams<<teamscores<<endl;


    }

    int averageQtrScore (int teamscores[][COLS])
    {


         int sum = 0;
         int j=1;
         int i=1;
         int homeavg;
         int visitoravg;

         for (i=0; i<ROWS; i++)
          {
             for (int j=0; j<COLS; j++)

             sum+=teamscores[i][j];

             homeavg = sum/COLS;
             visitoravg = sum/ROWS;

          }
          cout<<"Vistors average score is "<<visitoravg<<endl;
          cout<<"Home average score is "<<homeavg<<endl;

    }

    int highscore (int teamscores[][COLS])
    {

                int highestscore;

               highestscore = teamscores[ROWS][COLS];

               for (int i = 0; i<COLS; i++)

               if (teamscores[ROWS][COLS] < highestscore)
               highestscore = teamscores[ROWS][COLS];

               cout<<"Home team had the highest scoring quarter. They had "<<highestscore<<" points in quarter #3"<<endl;


    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Is anyone available to help me? I have to turn it in today at 5pm EST. I can't figure out why my finalScore function is getting me random numbers in my output.

Because readFile( ) is not reading or storing anything, which is then not being passed to the finalScore( ) function.

finalScore( ) is not yet walking through the array of scores. You cannot output an entire array in one statement, as you're doing. What that's displaying is the address of the array.

I thought it should get the information from teamscores that I put. Because I remember when u edited my program, u asked what else i should put there and the only thing I had declared was teamscores. I don't know. I'm confused.

[code=cplusplus]
void readFile (ifstream& inData,int teamscores[][COLS])
    {
           
         inData.open("Unknown.txt");

    }
    
    int finalScore(int teamscores[][COLS])
    {
        
        string teams[ROWS] = {"Visitors","Home"};
       
             cout<<teams<<teamscores<<endl;
    
   
    }

[/code]
void readFile (ifstream& inData,int teamscores[][COLS])
{
           
         inData.open("Unknown.txt");
         //test that your inData file handle successfully opened

        //read from inData, storing to teamscores
        //you need nested loops, outer for the teams, inner for the scores
   }
    
    int finalScore(int teamscores[][COLS], string teams[] )
    {
        
        //no input here
        //display each team's quarter scores, and add up the scores for total

        //again, nested loops, outer by team, inner by quarter score
      
    }

inData >> teamscores[team][qtr];

Repeat this in a loop for variable team, inside that a loop for variable qtr.

You have to read and store one element at a time, there is no way to read into the whole array directly.

Are these correct so far? or which ones I need to change or whatever.

int main()
{

    int teamscores[ROWS][COLS] = {{18,19,22,21},{22,23,25,17}};    
    string teams[ROWS] = {"Visitors","Home"};

AND

  void readFile (ifstream& inData,int teamscores[][COLS])
    {


         inData.open("Unknown.txt");
         inData>>teamscores[team][qtr];


    }
int finalScore(int teamscores[][COLS])
    {

     int sum = 0;

     for (i=0; i<COLS; i++)
     sum = sum+teamscores[i];

     for (j=0;i<COLS; j++)
     sum = sum+teamscores[j];


    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

void readFile (ifstream& inData,int teamscores[][COLS])
    {
        //declare variables team & qtr
  
         inData.open("Unknown.txt");

         //loop, using team as counter, to access each row
               //loop, using qtr as counter, to access each column 
                        inData>>teamscores[team][qtr];

        
    }
int finalScore(int teamscores[][COLS], string teams[]) //team array put back
    {
  
     int sum = 0;
     
     //use the same loop structure as shown above, now for displaying the array content
    //be sure to set sum back to 0 before starting to add up the second team

     //  for each row, use that index to display the name of the corresponding team

     for (i=0; i<COLS; i++)
     sum = sum+teamscores[i];
     
     for (j=0;i<COLS; j++)
     sum = sum+teamscores[j];
    
   
    }

sorry it took me awhile to respond. I had to drive to school....I have to turn in my assignment at 5 pm EST. It is going to be a close call!

Is this correct so far?

void readFile (ifstream& inData,int teamscores[][COLS])
{
     int team;
     int qtr;

     inData.open("Unknown.txt");

      for (team=0; team<ROWS; team++)
      {
         for (int qtr=0; qtr<COLS; qtr++)

         inData>>teamscores[team][qtr];
      }   


}

int finalScore(int teamscores[][COLS], string teams[])
{

    int sum = 0;
    int team;
    int qtr;


     for (team=0; team<ROWS; team++)
      {
       sum = sum+teamscores[team][qtr];           
       sum = 0;
     for (int qtr=0; qtr<COLS; qtr++)
         sum = sum+teamscores[team][qtr];


      }  

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Sorry, I misread the last statement you did. I corrected it, but I am getting a small error saying:

invalid conversion from 'int*' to 'int'  for "sum = sum+teamscores[i];"

Code:

int finalScore(int teamscores[][COLS], string teams[])
{

    int sum = 0;


     for (int i=0; i<COLS; i++)
      {
       sum = sum+teamscores[i];           
       sum = 0;
     for (int j=0; j<COLS; j++)
       sum = sum+teamscores[j];


      }   


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

teamScores is a 2D array, you must access its elements with row and column indices, just like you did in readFile( ).

Really, take the loop you have in readFile( ), copy it to finalScore, and modify what occurs in the body of the loop.

Is this an example of what I'm suppose to be putting in the for loop?

for (i=0; i<ROWS; i++)    
 sum = sum+teamscores[i];      
for (j=0;i<COLS; j++)     
 sum = sum+teamscores[j];

Edited 3 Years Ago by mike_2000_17: Fixed formatting

for (i=0; i<ROWS; i++)   
{ 
    sum = 0 ;
    for (j=0;i<COLS; j++)     
    {
        sum = sum+teamscores[i][j];
        cout << teamscores[i][j] << "  ";
     }
}

Have you been referring to your text's section on arrays/2D arrays?

Textbook and Notes from class. None of it is specific, it is just generalizing. It doesn't show how to do the inFile for Two-Dimensional arrays, so I'm pretty much trying to do it with little knowledge of it. Plus, I'm also trying to rush and beat the clock.

I tried to run the program, but I"m getting like infinite loop and than an error. What I did was when I put: int finalScore(int teamscores[][COLS], string teams[]), I added string teams[] to the function above main. Was I suppose to do that?

When I leave it out, I get the

[Linker error] undefined reference to `finalScore(int (*) [4])'  

error

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int ROWS = 2;
const int COLS = 4;


void readFile(ifstream& inData,int teamscores[][COLS]);
int finalScore(int teamscores[][COLS],string teams[]);
int averageQtrScore (int teamscores[][COLS]);
int highscore (int teamscores[][COLS]);


int main()
{

    int teamscores[ROWS][COLS] = {{18,19,22,21},{22,23,25,17}};    
    string teams[ROWS] = {"Visitors","Home"};

    ifstream inData;

    cout<<fixed<<showpoint;
    cout.precision(2);

    readFile(inData,teamscores);
    finalScore(teamscores,teams);
    averageQtrScore(teamscores);
    highscore(teamscores);

    inData.close();

    system ("PAUSE");
    return 0;
}

    void readFile (ifstream& inData,int teamscores[][COLS])
    {
         int team;
         int qtr;

         inData.open("Unknown.txt");

          for (team=0; team<ROWS; team++)
          {
             for (int qtr=0; qtr<COLS; qtr++)

             inData>>teamscores[team][qtr];
          }   


    }

    int finalScore(int teamscores[][COLS], string teams[])
{

      int sum = 0;  

      for (int i=0; i<ROWS; i++)   
      { 
      sum = 0 ;
    for (int j=0;i<COLS; j++)     
    {
        sum = sum+teamscores[i][j];
        cout << teamscores[i][j] << "  ";
     }
      }


}

    int averageQtrScore (int teamscores[][COLS])
    {


         int sum = 0;
         int j=1;
         int i=1;
         int homeavg;
         int visitoravg;

         for (i=0; i<ROWS; i++)
          {
             for (int j=0; j<COLS; j++)

             sum+=teamscores[i][j];

             homeavg = sum/COLS;
             visitoravg = sum/ROWS;

          }
          cout<<"Vistors average score is "<<visitoravg<<endl;
          cout<<"Home average score is "<<homeavg<<endl;

    }

    int highscore (int teamscores[][COLS])
    {

                int highestscore;

               highestscore = teamscores[ROWS][COLS];

               for (int i = 0; i<COLS; i++)

               if (teamscores[ROWS][COLS] < highestscore)
               highestscore = teamscores[ROWS][COLS];

               cout<<"Home team had the highest scoring quarter. They had "<<highestscore<<" points in quarter #3"<<endl;


    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Oh, this is also my first program with Two-Dimensional Arrays. One Dimensional was easier =/

in finalScore( ), you have wrong test in the inner loop for (int j=0;i<COLS; j++) This should have j < COLS It helps to put a blank space between operators and their operands - helps readability.

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