当输入 2x2 数组时,循环仅检查底行。
输入示例:
Input n, m:
2 2
Input a[2][2]:
1 2
3 4
Choose a number:
1
There is no such element
当输入 3x3 数组时,程序会提前结束并出现错误代码 139。
Input n, m:
3 3
Input a[3][3]:
1 2 3
4 5 6
7 8 9
这是程序:
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
int n, m, i, j, b, k;
float a[n][m];
cout << "Input n, m:\n";
cin >> n >> m;
cout << "Input a[" << n << "][" << m << "]:\n";
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cout << "Choose a number:\n";
cin >> b;
k = -1;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] == b) {
k = j;
break;
}
}
}
if (k >= 0)
cout << "Number of a column with a number: " << j << "\n";
else
cout << "There is no such element" << endl;
getch();
}