-5

In my code below, I try to find an 'x' in a 2D array and display its row and column. But it always shows the a[0][1] in the result. Can someone tell me where did I wrong and why is that? I tried other code but there is no difference. Thanks

My code:

#include <iostream>
using namespace std;
int main(){
    int m, n, i, j, a[m][n], sum, x, sl=0;
    cout<<"insert array number of rows: "; cin>>m;
    cout<<"insert array number of columns: "; cin>>n;
    for(i=0;i<m;i++){
        sum=0;
        for(j=0;j<n;j++){
            cout<<"insert board["<<i<<"]["<<j<<"]: ";
            cin>>a[i][j];
            sum+=a[i][j];
        }
        cout<<"the summary of rows "<<i<<" is: "<<sum<<endl;
    }
    cout<<"search x= "; cin>>x;
    for (i=0; i<=m-1; i++){
        for (j=0; j<=n-1; j++){
            if (a[i][j]==x){
                sl++;
                cout<<"x is at "<<"a["<<i<<"]["<<j<<"]"<< endl;
            }
        }
    }
    if(sl==0){
        cout<<"there is no x in the array";
    }
    cout<<sl;
    return 0;
}

My results:

MyResult

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    pleas post your code and input output as text in the question not as link, you can **add** link buy but not insted – yaodav Jul 14 '20 at 08:08
  • Pay attention to compiler warnings: `prog.cc:4:27: warning: 'n' is used uninitialized [-Wuninitialized]`, `prog.cc:4:27: warning: 'm' is used uninitialized [-Wuninitialized]`. – brc-dd Jul 14 '20 at 08:24

1 Answers1

1

Sources of error in your code:

You were using a variable length array (which is not a part of C++ standard). Moreover you were setting its rows and columns before even initializing/inputting n and m.

Fixed Code:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int m, n, x, sl = 0;

    cout << "Insert number of rows in the array: ";
    cin >> m;
    cout << "\nInsert number of columns in the array: ";
    cin >> n;

    // put the below line after cin >> m; cin >> n;
    vector<vector<int>> a(m, vector<int>(n, 0));  // <-- Use this instead of int a[m][n];

    cout << endl;
    
    for (int i = 0; i < m; ++i) {
        int sum = 0;
        for (int j = 0; j < n; ++j) {
            cout << "\nInsert board[" << i << "][" << j << "]: ";
            cin >> a[i][j];
            sum += a[i][j];
        }
        cout << "\nThe sum of the row " << i << " is: " << sum << endl;
    }

    cout << "\nSearch x = ";
    cin >> x;
    
    cout << '\n' << endl;

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == x) {
                sl++;
                cout << "x is at a[" << i << "][" << j << "]\n";
            }
        }
    }

    if (sl == 0) { cout << "\nThere is no x in the array!"; }
    else cout << '\n' << sl;

    return 0;
}

Sample Input :

2
2
3
4
7
3
3

Sample Output :

Insert number of rows in the array: 
Insert number of columns in the array: 

Insert board[0][0]: 
Insert board[0][1]: 
The sum of the row 0 is: 7

Insert board[1][0]: 
Insert board[1][1]: 
The sum of the row 1 is: 10

Search x = 

x is at a[0][0]
x is at a[1][1]

2

For interactive run, you can remove the newline characters at your wish.

brc-dd
  • 10,788
  • 3
  • 47
  • 67