Is my functions correct? If I type in the highest temp at the bottom row, my program wouldn't take it. Same goes for lowest temp.

Can anyone provide some in sight?

#include <iostream>

using namespace std;

//declare constant for array temperature
int const row = 3;
int const col = 2;

double temp[row][col];


//function prototypes
void getData(double temp[row][col]);
void avghigh(double temp[row][col]);
void avglow(double temp[row][col]);
void hitemp(double temp[row][col]);
void lotemp(double temp[row][col]);

int HIGH = 0;
int LOW = 1;

//main function
int main()
{
    double temp[row][col];

    //calling function for data input
    getData(temp);
    //calling the functions to display average and hi/lo temp.
    avghigh(temp);
    avglow(temp);
    hitemp(temp);
    lotemp(temp);

    system("pause");
    return 0;
}


//all functions definetion
void getData(double temp[row][col])
{                                                     
    int i;

     cout << "Enter high temperature." << endl;  //user inputs for each month. highest temperature in first row

    for (i=0 ; i<row; i++)
    {
        cout << "Month: " << (i+1) << " ";
        cin >> temp[i][HIGH];   
    }

    cout << "Enter low temperature." << endl;   //user inputs for each month. lowest temp in first row

    for (i=0 ; i<row; i++)
    {
        cout << "Month: " << (i+1) << " ";
        cin >> temp[i][LOW];
    }
}

void avghigh(double temp[row][col])
{
    int i;         
    int Sum = 0;                               
    double Average;                                  

    for (i=0; i<row; i++)
    {
        Sum = temp[i][0] + Sum;                                 
    }

    Average = Sum/i;

    cout << "Average high temperature for the whole year is: " << Average << endl;
}

void avglow(double temp[row][col])
{
    int Sum = 0;
    int i;
    double Average;      

    for (i=0; i<row; i++)
    {
        Sum = temp [i][1] + Sum;
    }

    Average = Sum/i;

    cout << "Average low temperature for the whole year is: " << Average << endl;
}

void hitemp(double temp[row][col])
{
    int highesttemp = 0; 
    int i;

    for(i=0; i<row; i++)
    {
        if(temp[0][i] > highesttemp)
            highesttemp = temp[0][i];
    }

    cout << "The highest temperature is: " << highesttemp << endl;
}

void lotemp(double temp[row][col])
{
    int lowesttemp = 0;     
    int i;

    for(i=0; i<row; i++)
    {
        if(temp[0][i] < lowesttemp)
            lowesttemp = temp[0][i];
    }       

    cout << "The lowest temperature is: " << lowesttemp << endl;
}

Recommended Answers

All 8 Replies

You are checking the lowesttemp and highesttemp as zero to begin with you need to initialise them as the first element of your chosen array col. So add lowesttemp = temp[LOW][0]; before the for loop and the same with the highesttemp = temp[HIGH][0]; before the for loop again see here:

void hitemp(double temp[row][col])
    {
        int highesttemp = 0;
        int i;

        highesttemp = temp[HIGH][0];
        for(i=0; i<row; i++)
        {
            if(temp[HIGH][i] > highesttemp)
            highesttemp = temp[HIGH][i];
        }

        cout << "The highest temperature is: " << highesttemp << endl;
    }

    void lotemp(double temp[row][col])
    {
        int lowesttemp = 0;
        int i;

        lowesttemp = temp[LOW][0];
        for(i=0; i<row; i++)
        {
            if(temp[LOW][i] < lowesttemp)
            lowesttemp = temp[LOW][i];
        }

        cout << "The lowest temperature is: " << lowesttemp << endl;
    }

Plus continue using your labled defines, HIGH and LOW instead of using raw values you will make less mistakes that way and the code will be easier to follow.

I have added what you suggested by initializing the element to 0. But it doesn't work.

I have tried 3 different scenarios putting hi/lo inputs in 3 different rows in the array and the program won't take hi/lo input if placed in index#1 or index#2 in the array. But if i place the obvious hi/lo input in index#0 then my hi/lo function will display the correct number. But in reality that is not how it should work.

Help.

i dont understand what you mean by putting hi/lo inputs. The program code I listed above you works!!! I tested it...!

 void lotemp(double temp[row][col])
{
    int lowesttemp = 0;
    int i;

    lowesttemp = temp[LOW][0]; // <=== Here is what you needed !!!
    for(i=0; i<row; i++)
    {
        if(temp[LOW][i] < lowesttemp)
        lowesttemp = temp[LOW][i];
    }
    cout << "The lowest temperature is: " << lowesttemp << endl;
}

Now the lotemp(...) function works

I did edit both hitemp and lotemp functions and added line 6 of your suggestions.

By hi/lo, I mean typing user inputs, as in actually putting in the random numbers in random rows. Have you tried putting random highest and lowest number in the last row of the array.

*If you place your highest or lowest user input in the last row of the array, the result displayed is wrong.

Or maybe I am out of coffee and I am seeing things.

You have the indices swapped when accessing the elements of temp in the functions hitemp and lowtemp. You have:

temp[ HIGH ][ i ];

you should have:

temp[ i ][ HIGH ];

You could try making a Range structure and keep an array of those instead:

struct Range
{
    double low, high;
};

Range tempRanges[ 3 ];

You'll then avoid the confusion of your indices:

tempRange[ i ].low = x;
tempRange[ i ].high = y;

or

tempRange[ i ] = { -10, 20 };

We haven't cover struct as a class.

But is this what you meant,

void hitemp(double temp[row][col])
{
    int highesttemp = 0; 
    int i;

    highesttemp = temp[HIGH][0]; //added as suggested by elix
    for(i=0; i<row; i++)
    {
        if(temp[0][i] > highesttemp) //ravenous is this the line that? to change to temp[i][HIGH]
            highesttemp = temp[0][i]; //this also?
    }

    cout << "The highest temperature is: " << highesttemp << endl;
}

void lotemp(double temp[row][col])
{
    int lowesttemp = 0;     
    int i;

    lowesttemp = temp[LOW][1]; //as suggested by elix
    for(i=0; i<row; i++)
    {

        if(temp[0][i] < lowesttemp) //ravenous is this the line that? to change to temp[i][LOW]
            lowesttemp = temp[0][i]; //this also?
    }       

    cout << "The lowest temperature is: " << lowesttemp << endl;
}

It works thank you very much guys.

It will work

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.