mardi 30 juin 2020

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?

Aucun commentaire:

Enregistrer un commentaire