简体   繁体   中英

I'm trying to make my 2-D Array program to get the largest element in each row but its not working

This is my code

#include <iostream>
using namespace std;
const int rows = 2;
const int cols = 6;

void CheckRow(int x[][6], int rows){

        int highestr = 0;

        int r = 0, c = 0;
        for (r = 0; r < rows; ++r){
                cout << "ROW " << r + 1 << ": ";
                for (c = 0; c < cols; ++c)
                cout << x[r][c] << " ";
                cout << endl;
        }
> End of PRINTING THE CONTENTS

> THE CODE BELOW MUST CHECK FOR THE GREATEST VALUE IN A ROW, BUT IT WONT GIVE ME THE EXACT RESULTS AS IT SHOULD DISPLAY

        for (r = 0; r < rows; ++r){
                for (c = 0; c < cols; ++c)
                if (x[r][c] > highestr){
                        highestr += x[r][c];
                        cout << " Greatest Number: " << highestr;
                        cout << endl;
                }
        }
}

int main()
{
        int temps[rows][cols] =
        {
                { 12, 13, 14, 15, 16, 17 },
                { 18, 19, 20, 21, 22, 23 }
        };

        CheckRow(temps, 2);

        cout << endl << endl;

        system("pause");
        return 0;
}
for (r = 0; r < rows; ++r)
{
         for (c = 0; c < cols; ++c)
         {
            if (x[r][c] > highestr)
            {
                    highestr = x[r][c];
            }
         }
         cout << " Greatest Number: " << highestr;
         cout << endl;
         highestr=0; // considering every element in your 2D array is greater than 0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM