我正在尝试使我的 2-D 数组程序获取每行中最大的元素,但它不起作用

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

本文关键字:元素 不起作用 获取 我的 程序 数组 2-D      更新时间:2023-10-16

这是我的代码

#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
}