Ok, I have a program I'm trying to get to work, but I'm struggling to get right. I suspect its when it reads the data from the .txt file. I have to get the average of column 1 and the average of column 2, and get the highest of column 1 and the lowest of column 2.

Here's the code I have so far...

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


using namespace std;

int getData();
int averageHigh();
int averageLow();
int indexHighTemp();
int indexLowTemp();

int i, j;
int hsum = 0, lsum = 0;
int avgh, avgl;
int high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
int temp[row][col];

int main()
{
    cout << "Processing Data" <<endl;
    cout << endl;
    getData();
    averageHigh();
    averageLow();
    indexHighTemp();
    indexLowTemp();
    cout << " " <<endl;

  system("PAUSE");
  return 0;
}
int getData()
{
    ifstream inFile;
       
    inFile.open ("c:\\Ch9_Ex9Data.txt");
       
    if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 0;
       }  
      
     while( inFile >> temp[i][0] >> temp[i][1])
    {
    ++i;
    }

}
int averageHigh()
{
    for(int i = 0; i < row; i++)
       {
            for(int j=0; j < col; j++)
               {
                    hsum = (hsum + (temp[0][i]));
               }
       }
       avgh = hsum / 12;
       cout << "Average high temperature: ";
       cout << avgh <<endl;
       return 0;
}
int averageLow()
{
    for(int i=0;i<row;i++)
       {
            for(int j=0;j<col;j++)
               {
                    lsum = (lsum + (temp[1][i]));
               }
       }
       avgl = lsum / 12;
       cout << "Average low temperature: ";
       cout << avgl << endl;
       return 0;
       }
int indexHighTemp()
{
    ind = 0;
       for(int i=0; i < row; i++)
       {
        for(int j = 0; j < col; j++)
          {
            if (high <= (temp[0][i]))
               {
                     high = (temp[0][i]);
                     ind = i;
               }
            }
        }
       cout << "Highest temperature: ";
       cout << (temp[0][ind]) << endl;
       return 0;
}
int indexLowTemp()
{
    ind = 0;
       for(int i=0; i < row; i++)
       {
        for(int j = 0; j < col; j++)
        {
         if (low >= (temp[1][i]))
            {
             low = (temp[1][i]);
             ind = i;
            }
         }
        }                  
       cout << "Lowest temperature: ";
       cout << (temp[1][ind]) << endl;
       return 0;
}

And here's the .txt file data

34 -12
30 -21
32 -18
40 -5
52 10
75 32
85 49
92 53
88 31
65 9
42 -17
39 -19

The lowest number is right, but the highest is not. Plus my averages are way off. Again, I think its my getData() function.

Help! Thanks in advance.

well a really easy way to fix this is if you can use 2 one dimensional arrays then store the first column in one array and the other column in the second array

int column1[12], column2[12];
int i = 0;
// open the file
while (!inFile.eof()) // goes
{
infile >> column1[i] >> column2[i];
i++;
}

this will also make finding data int the separate columns a lot easier. the problem you were having with your 2d array was on the line while( inFile >> temp[i][0] >> temp[i][1]) . here you were writing data to places that didn't exist because the array was defined as temp[2][12] but this line was going up to temp[11][0] and temp[1][11]. the later was good but the first one you would want the i variable in the second part of the array like temp[0]. this way you would go up to temp[0][11] and temp [1][11]. i hope this makes sense.

looking at your .txt file, you have the row and column backwards
in declaring your tmp array variable.

Sweet, I'm a little closer. Thank you for all your help. I have the highest and lowest come out ok, but my averages are still off. Here's the changes I made to the code:

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


using namespace std;

int getData();
int averageHigh();
int averageLow();
int indexHighTemp();
int indexLowTemp();

int i, j;
int hsum = 0, lsum = 0;
int avgh, avgl;
int high = 0, low = 0;
int ind;
int const row = 12, col = 2, num = 12;
int temp[col][row];

int main()
{
    cout << "Processing Data" <<endl;
    cout << endl;
    getData();
    averageHigh();
    averageLow();
    indexHighTemp();
    indexLowTemp();
    cout << " " <<endl;

  system("PAUSE");
  return 0;
}
int getData()
{
    ifstream inFile;
       
    inFile.open ("c:\\Ch9_Ex9Data.txt");
       
    if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 0;
       }  
      
     while( inFile >> temp[0][i] >> temp[1][i])
    {
    ++i;
    }

}
int averageHigh()
{
    for(int i = 0; i < row; i++)
       {
            for(int j=0; j < col; j++)
               {
                    hsum = (hsum + (temp[0][i]));
               }
       }
       avgh = hsum / 12;
       cout << "Average high temperature: ";
       cout << avgh <<endl;
       return 0;
}
int averageLow()
{
    for(int i=0;i<row;i++)
       {
            for(int j=0;j<col;j++)
               {
                    lsum = (lsum + (temp[1][i]));
               }
       }
       avgl = lsum / 12;
       cout << "Average low temperature: ";
       cout << avgl << endl;
       return 0;
       }
int indexHighTemp()
{
    ind = 0;
       for(int i=0; i < row; i++)
       {
        for(int j = 0; j < col; j++)
          {
            if (high <= (temp[0][i]))
               {
                     high = (temp[0][i]);
                     ind = i;
               }
            }
        }
       cout << "Highest temperature: ";
       cout << (temp[0][ind]) << endl;
       return 0;
}
int indexLowTemp()
{
    ind = 0;
       for(int i=0; i < row; i++)
       {
        for(int j = 0; j < col; j++)
        {
         if (low >= (temp[1][i]))
            {
             low = (temp[1][i]);
             ind = i;
            }
         }
        }                  
       cout << "Lowest temperature: ";
       cout << (temp[1][ind]) << endl;
       return 0;
}

the problem with your averages is that you are using i instead of j in

for(int i = 0; i < row; i++)
{
     for(int j=0; j < col; j++)
        {
             hsum = (hsum + (temp[0][i])); <--  USING i SHOULD BE j
        }
}

if you change i to j on this line in both your AverageHigh() and AverageLow() functions it should work fine. as a thought though you really don't need to loops here it could easily be written as

//...
for (int i = 0; i < col; i++)
{
       hsum = (hsum + temp[0][i]);
}
avgh = hsum / col; // or 12 whatever you prefer
//...

as a note i used the variable col here to tell the loop how many times to run and also for the division. this is a more preferred way to program because if you want to change col to some other number i.e. 20 you would have to change the constants in your program that are 12 to 20 otherwise you will have another bug to fix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.