使用我的代码,我可以在矩阵中找到最大的数字,但是找不到数字的确切位置(在行和列中)。我是一个初学者,所以我不了解任何精美的库,并且我不愿意使用它们,因为我还没有研究它们及其4X4矩阵 这是我的代码
#include<iostream>
using namespace std;
int main()
{
int a[4][4], big1, n, m, i, j,loc1,loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is " << loc1 <<" " << loc2 << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
您只是缺少一些括号来仅最大更新位置变量:
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
答案 1 :(得分:0)
一切都用您的代码编写。小事需要在if条件后更新大括号
在这里他更新了代码
onDeleteTask(id){
this.setState({taskId:id});
this.deleteTask(this.state, this.onDeleteAttempt);
}