2D(每列的最大值)

2D (Maximum From Each Column)

本文关键字:最大值 2D      更新时间:2023-10-16

经过多次尝试并反复思考。感觉就像一个沮丧的人。我在这里从你们那里得到一些建议..

实际上,我正在尝试找到每个行和列的最大值。因此,通过使用分而治之技术,我编写了两个单独的函数,一个用于查找每行的最大值,并将其存储到我用作参数的行向量中。分别用于列。

void maxValuesR(int a[][cols], int rv[], int row)
void maxValuesC(int a[][cols], int cv[], int row)

问题:代码甚至没有编译,我只是不理解错误。请帮忙..

我真的需要你的帮助!

代码如下:

#include <iostream>
using namespace std;
const int rows = 2;     // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            cout << "Enter Element: ";
            cin >> arr[i][j];
        }
    }
}
void printData(int a[][cols], int rows)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            cout << a[i][j] << "t";
        }
        cout << endl;
    }
}
void print_single(int a[], int row)
{
    for(int i = 0; i < row; i++)
    {
        cout << a[i] << "t";
    }
}
void maxValuesR(int a[][cols], int rv[], int row)
{   int foxi;
    for(int i = 0; i < row; i++)
    {
        foxi = a[i][0];
        for(int j = 0; j < cols; j++)
            {
                if(foxi < a[i][j])
                {
                    foxi = a[i][j];
                }
            }
            rv[i] = foxi;
    }
}
void maxValuesC(int a[][cols], int cv[], int row)
{
    int maxi;
    for(int i = 0; i < cols; i++)
    {
        maxi = a[0][i];
            for(int j = 0; j < row; j++)
            {
                if(maxi > a[j][i])
                {
                    maxi = a[j][[i];// show error here => expected a '{' introducing lambda body
                }
            }
            cv[i] = maxi;   
    }
}
int main()
{
    int rowVector[rows];
    int colVector[cols];
    int a[rows][cols];
    cout << "Fill Array_1. " << endl;
    getData(a, rows);
    cout << "Array_1." << "nn";
    printData(a, rows); cout << endl;
    maxValuesR(a, rowVector, rows);
    print_single(rowVector, rows);
    cout << "nn";
    maxValuesC(a, colVector, rows);
    print_single(colVector, rows);
    return 0;
}

要澄清编译错误:

  • 您忘记了包含(或者没有在这里写? #include <iostream>
  • 您没有为 cincoutendl 指定命名空间(它们位于std命名空间中)
  • 你在语句中有一个多余的"["a[j][[i],你很可能想写a[j][i]

编译代码如下所示:

#include <iostream>
const int rows = 2;     // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            std::cout << "Enter Element: ";
            std::cin >> arr[i][j];
        }
    }
}
void printData(int a[][cols], int rows)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            std::cout << a[i][j] << "t";
        }
        std::cout << std::endl;
    }
}
void print_single(int a[], int row)
{
    for(int i = 0; i < row; i++)
    {
        std::cout << a[i] << "t";
    }
}
void maxValuesR(int a[][cols], int rv[], int row)
{   int foxi;
    for(int i = 0; i < row; i++)
    {
        foxi = a[i][0];
        for(int j = 0; j < cols; j++)
            {
                if(foxi < a[i][j])
                {
                    foxi = a[i][j];
                }
            }
            rv[i] = foxi;
    }
}
void maxValuesC(int a[][cols], int cv[], int row)
{
    int maxi;
    for(int i = 0; i < cols; i++)
    {
        maxi = a[0][i];
            for(int j = 0; j < row; j++)
            {
                if(maxi > a[j][i])
                {
                    maxi = a[j][i];
                }
            }
            cv[i] = maxi;   
    }
}
int main()
{
    int rowVector[rows];
    int colVector[cols];
    int a[rows][cols];
    std::cout << "Fill Array_1. " << std::endl;
    getData(a, rows);
    std::cout << "Array_1." << "nn";
    printData(a, rows);
    std::cout << std::endl;
    maxValuesR(a, rowVector, rows);
    print_single(rowVector, rows);
    std::cout << "nn";
    maxValuesC(a, colVector, rows);
    print_single(colVector, rows);
    return 0;
}

但是,无法判断它是否生成了您想要的输出,因为您没有指定任何示例输入(更不用说相应的预期输出了)......