2D(每列的最大值)
2D (Maximum From Each Column)
经过多次尝试并反复思考。感觉就像一个沮丧的人。我在这里从你们那里得到一些建议..
实际上,我正在尝试找到每个行和列的最大值。因此,通过使用分而治之技术,我编写了两个单独的函数,一个用于查找每行的最大值,并将其存储到我用作参数的行向量中。分别用于列。
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>
- 您没有为
cin
、cout
和endl
指定命名空间(它们位于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;
}
但是,无法判断它是否生成了您想要的输出,因为您没有指定任何示例输入(更不用说相应的预期输出了)......
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- 如何使用函数参数中的int指针来获取2d数组中最大值的索引并返回指针
- C++ 在 2D std::vector 中查找最大值的位置
- 将 3D 坐标转换为 3ds 中的 2D 最大值
- 查找最大值并删除 2D 数组中的该行.C++
- 2D(每列的最大值)
- 获取 2D 数组中数字的平均、最小值和最大值
- 我想找到一个2d数组C++中每一行的最大值
- 求2D数组C++中每一行的最大值