shehab ahmed
shehab ahmed

Reputation: 39

How to get the lowest number of one column in a two dimensional array in C++?

I have a simple program to get from the user the lowest & highest temperature, then I must make the program get the average of the lowest & highest temperature of the day. I should put the 7days,low,high in a two dimensional arrays name temp `temp[7][2]={0};. so I made the program and it's work well and this is the code;

    //declare arr & varaibles 
    int temp[7][2]={0}; // temperature array 
    double totalHigh=0,totalLow=0; //total low temp days & total high temp days
    int high=temp[0][0],low=temp[0][0];// assign the first row & column in array to get the highest & lowest number
    
    for(int row=0;row<7;row+=1){ // for loop to get through every row 
        cout << "Day " << row+1 << endl; 
        cout << "Low: "; cin >> temp[row][0]; // first column is for low temp days
        totalLow += temp[row][0] ; // get the total low days from the user
        cout << "High: "; cin >> temp[row][1]; // second column is for high temp days
        totalHigh += temp[row][1]; // get the total high days from the user
    }

    for(int j=0;j<7;j+=1){ // go through first column to get the high number of it
        if(high < temp[j][0]){
            high = temp[j][0];
        }
    }

    for(int j=0;j<7;j+=1){ // go though second column to get the low number of it
        if(low > temp[j][1]){
            low = temp[j][1];
        }
    }

    //display on screen 
    cout << fixed<< setprecision(2);
    cout << "Low: "<< totalLow/7 << endl; //get the average of low
    cout << "High: " <<totalHigh/7 << endl;// get the average of high
    cout << "Lowest Of High column: " << low << endl;
    cout << "Highst Of Low column: " << high << endl;

but I should also get the highest number of low column & the lowest number of high column. i get the lowest number of high column, but when I make the loop to get the lowest number it doesn't work. this is the code of the two loops that go through every column and get them;

    for(int j=0;j<7;j+=1){ // go through first column to get the high number of it
        if(high < temp[j][0]){
            high = temp[j][0];
        }
    }

    for(int j=0;j<7;j+=1){ // go though second column to get the low number of it
        if(low > temp[j][1]){
            low = temp[j][1];
        }
    }

but second loop is not working while the first loop is working, can anyone tell me why the second loop is not working?

Upvotes: 1

Views: 722

Answers (4)

brc-dd
brc-dd

Reputation: 13044

Because of the c++17 tag I am suggesting this as much better and modern (STLish) solution:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

int main() {

    std::vector<std::pair<int, int>> temp(7);
    
    for (int i = 0; i < temp.size(); ++i) {
        std::cout << "Day " << i + 1 << std::endl;
        std::cout << "Low: ";
        std::cin >> temp.at(i).first;
        std::cout << "High: ";
        std::cin >> temp.at(i).second;
    }
    
    std::cout << std::fixed << std::setprecision(2);
    
    std::cout << "Average Low: " << (std::accumulate(temp.begin(), temp.end(), 0.0F, [](auto &a, auto &b) { return a + b.first; })) / temp.size() << std::endl;
    std::cout << "Average High: " << (std::accumulate(temp.begin(), temp.end(), 0.0F, [](auto &a, auto &b) { return a + b.second; })) / temp.size() << std::endl;
    
    auto [low_low, high_low]   = std::minmax_element(temp.begin(), temp.end(), [](auto &a, auto &b) { return a.first < b.first; });
    auto [low_high, high_high] = std::minmax_element(temp.begin(), temp.end(), [](auto &a, auto &b) { return a.second < b.second; });
    
    std::cout << "Lowest low temperature: " << (*low_low).first << std::endl;
    std::cout << "Highest low temperature: " << (*high_low).first << std::endl;
    std::cout << "Lowest high temperature: " << (*low_high).second << std::endl;
    std::cout << "Highest high temperature: " << (*high_high).second << std::endl;
    
    return 0;
}

Upvotes: 1

Ayush Jain
Ayush Jain

Reputation: 181

int high=INT_MIN,low=INT_MAX;

This will give you lowest of the high column and highest of the low column.

Upvotes: 0

UnclePooh
UnclePooh

Reputation: 78

I think that the problem is your low variable initialization.

low=temp[0][0];

You can modify this loop in this way:

auto low = temp[0][1];
for(int j=1;j<7;j+=1) // go though second column to get the low number of it
    if(low > temp[j][1])
        low = temp[j][1];

And it should works correctly. I think that start value of low is too low and that's why the lowest value from the highest temperature cannot be found.

Upvotes: 0

Ranoiaetep
Ranoiaetep

Reputation: 6657

The problem here is that you have init low to 0.

When you first init the 2d array, all values are set to 0. Then you have init low to temp[0][0], which means low is 0 now. Unless your lowest high is actually below 0, then it would never be updated.

Similarly, if your highest low is below 0, you would also notice that the high is not working properly either.

A way you can fix it is that you only init the high and low after user already input all the data. And init high = temp[0][0], low = temp[0][1]

Upvotes: 1

Related Questions