problem with an if statement in 2d array

hey guys am working on a program that returns the position of an element in a 2d array...the program works perfactly expcept for the part where if there element in not found it must return -1....when i impliment it in my loop i recieve multiple -1 ansas when i only need 1.....please help me with an if statement that works 4 such...thanx in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  #include <iostream>
#include <vector>
#include <fstream>
#include <cmath>

using namespace std;
int matrix(vector<int>&data,int x)
{
    int d=0;
    ifstream infile("input.txt");
    //////////////////////////////////infiling
    int arr[20][20];
    infile>>x;
    while(infile>>d)
    {
        data.push_back(d);
    }
    ///////////////////////////////////store in a 2d array
    int counter=0;
    int asize=sqrt(data.size());
    for(int i=0;i<asize;i++)
    {
        for(int j=0;j<asize;j++)
        {
            arr[i][j]=data.at(counter);
            counter++;
        }
    }
    ///////////////////////////////////finding the position
    int row=0;
    int col=0;
    for(int i=0;i<asize;i++)
    {
        for(int j=0;j<asize;j++)
        {
            if(arr[i][j]==x)
            {
                row=i++;
                //return row;


            }
        }
        return row;

    }
    //////////////////////////////////
    for(int i=0;i<asize;i++)
    {
        for(int j=0;j<asize;j++)
        {
            if(arr[i][j]==x)
            {
                col=j++;
                //return col;

            }
        }
      return col;
    }

    ////////////////////////////////
    cout<<row<<" "<<col<<endl;

}

int main()
{
    vector<int>data;
    int x=0;
    int flag=0;
    int row=0,col=0;
    flag=matrix(data,x);
    if(flag)
        cout<<row<<" "<<col<<endl;
    return 0;
}
Topic archived. No new replies allowed.