我这里有一个代码,可以让用户将数字输入到 3*4 矩阵中,该函数将确定用户输入的最大元素的索引位置。
这是所需的输出:
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at (1, 2)
我的代码的问题在于它为最大数组打印了错误的坐标。如果我采用上面显示的示例输出,则该位置的打印输出将是 (2,3)
。我尝试的是在函数的 for 循环中减去 i 和 j 的值,但这仅适用于特定输入,一旦我开始用完全不同的一组数字更改它,坐标索引将不准确再次。
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(const double a[][4], int location[]);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main(){
int location [2];
double matrix [ROW_SIZE][COLUMN_SIZE];
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
cout<<" "<< endl;
locateLargest(matrix, location);
}
void locateLargest(const double a[][4], int location[]){
int i, j;
double max= a[0][0];
location[0]= 0;
for(i = 1; i < ROW_SIZE; ++i){
for(j = 0; j < COLUMN_SIZE; j++){
if(max < a[i][j])
location[0] = i;
location[1]= j;
max= a[i][j];
}
}
cout << "The location of the largest element is at ("<< (location[0]) << " , "<< (location[1]) <<" )"<< endl;
}
答案 0 :(得分:1)
在这个 for 循环中
for(i = 1; i < ROW_SIZE; ++i){
您完全跳过了矩阵的第一行。
而且你忘记初始化数组位置的第二个元素。
location[0]= 0;
也在这个 if 语句中
if(max < a[i][j])
location[0] = i;
location[1]= j;
max= a[i][j];
你错过了大括号。必须有
if(max < a[i][j])
{
location[0] = i;
location[1]= j;
max= a[i][j];
}
在整个程序中,您都使用幻数 4
而不是常量 COLUMN_SIZE
。
并且该函数不应输出任何消息。决定是否输出消息的是函数的调用者。
该函数可以如下所示,如下面的演示程序所示。
#include <iostream>
#include <utility>
const size_t ROW_SIZE = 3;
const size_t COL_SIZE = 4;
std::pair<size_t, size_t> locateLargest( const double a[][COL_SIZE], size_t m )
{
std::pair<size_t, size_t> location( 0, 0 );
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = i == 0; j < COL_SIZE; j++ )
{
if ( a[location.first][location.second] < a[i][j] )
{
location = { i, j };
}
}
}
return location;
}
int main()
{
double a[ROW_SIZE][COL_SIZE] =
{
{ 23.5, 35, 2, 10 },
{ 4.5, 3, 45, 3.5 },
{ 35, 44, 5.5, 9.6 }
};
auto location = locateLargest( a, ROW_SIZE );
std::cout << "The location of the largest element "
<< a[location.first][location.second] << " is at ("
<< location.first << " , "<< location.second << " )"
<< '\n';
return 0;
}
程序输出为
The location of the largest element 45 is at (1 , 2 )
更通用的函数定义可以如下所示,如下面的演示程序所示。
#include <iostream>
#include <utility>
template <typename T, size_t M, size_t N>
std::pair<size_t, size_t> locateLargest( const T ( &a )[M][N] )
{
std::pair<size_t, size_t> location( 0, 0 );
for ( size_t i = 0; i < M; i++ )
{
for ( size_t j = i == 0; j < N; j++ )
{
if ( a[location.first][location.second] < a[i][j] )
{
location = { i, j };
}
}
}
return location;
}
int main()
{
const size_t ROW_SIZE = 3;
const size_t COL_SIZE = 4;
double a[ROW_SIZE][COL_SIZE] =
{
{ 23.5, 35, 2, 10 },
{ 4.5, 3, 45, 3.5 },
{ 35, 44, 5.5, 9.6 }
};
auto location = locateLargest( a );
std::cout << "The location of the largest element "
<< a[location.first][location.second] << " is at ("
<< location.first << " , "<< location.second << " )"
<< '\n';
return 0;
}
程序输出和上图一样就是
The location of the largest element 45 is at (1 , 2 )