0

My program reads the data from the infile. But outputs the wrong results accept for lowest temperature -21 which is correct. I have played with the while loop where I believe the problem lies but have been unsuccessful in getting the right results. Can somebody steer me in the right direction. I have been learning alot from this program, but not enough to make it work.

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

My code so far:

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


using namespace std;

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

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

int main()
{
    cout <<"Processing Data"<<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 1;
             } 
      while(inFile >> temp[i][0] >> temp[i][1] >> temp[i][2])
{
    ++i;
}
       
       }
double 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;
       }
double 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;
       }
double 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;
       }
double 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;
       }
2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by ArkM
0

Alas, your program has too rigid architecture including disposable functions with incorrect logic.
Why do you write all functions without parameters? As far as I know, your task assignment includes a phrase "with a proper parameters" (or what else). Why these functions process global data only?

Let's consider your indexHighTemp code (with corrected indentation):

double 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;
}

Some questions:
1. Why this function return type is double? Obviously , index values are int.
2. Why it returns 0 in every case?
3. Why it prints the (incorrect) highest temperature instead of an index of this value?
4. Why it prints anything at all? Only its callee (main in this case) knows what to do with requested result.
5. Why it has two nested loops? Obviously, high temperatures are placed in one and only one row of temp 2D array, so it's 1D array and we need one loop only to iterate over all elements.
6. Why the function uses the (external) high variable? Who set the right value of the high variable? Is it a correct value? If so, why the function changes it?

And so on...

See possible enchanced variant of indexHighTemp:

const int HIGH = 0;
const int LOW  = 1;
...
int indexHighTemp()
{
    int index = 0;
    double highes = temp[HIGHT][0];
    for (int i = 1; i < 12; ++i)
    {
        if (temp[HIGH][i] > highest)
        {
            highest = temp[HIGH][i];
            index = i;
        }
    }
    return index;
}

Now let the main function (or anybody else) prints obtained result...
However it's not the best (but correct) variant, see:

// Universal function for index of highest value search
int indexOfHighest(double t[], int n)
{
    int index = -1;
    if (n > 0 && t != 0)
    {
        double highest = t[index = 0];
        for (int i = 0; i < n; ++i)
        {
            if (t[i] > highest)
                highest = t[index = i];
        }
    }
    return index;
}
// Specialized function to search in month by month array
int indexHighTemp(double t[][12])
{
    return indexOfHighest(t[HIGH]);
}

If you have 2D array (for example, temp[2][12] then temp[i] refers to i-th row and is a denotation of 1D array.

Try to rewrite your program (it's not so hard work, even for beginner;)). You have a good chance to get a correct and well-designed program. The presented code is a terrible one (in all respects)...

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.