search an element in a 2d array

hello guys am trying to search a specific value in my matrix if its der than output value is in matrix if not output value is not in matrix...........but my code is not working plzzzzzzzzzzzzzzz help me

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
 #include <iostream>
#include <fstream>

using namespace std;
int searchmatrix(int x,int y)
{
    ifstream infile("input.txt");
    int arr[20][20];
    infile>>x;
    infile>>y;
    int i,j;
 //////////////////////////////////////////infiling to text file
    for(i=0;i<x;i++)
    {
        for(j=0;j<x;j++)
        {
            infile>>arr[i][j];
        }
    }
    ////////////////////////////////////////////search mtrix
    for(i=0;i<x;i++)
    {
        for(j=0;j<x;j++)
        {
            if(arr[i][j]!=y) return 1 ;

        }
    }
    
}

int main()
{
    int x=0;
    int y=0;
    int flag;
    flag=searchmatrix(x,y);

if(flag)
{

    cout<<"value is in matrix"<<endl;

}
else
    cout<<"value is not in matrix"<<endl;


    return 0;
if(arr[i][j]!=y) return 1 ;
"When I encounter any value which is not equal to y, return 1"
IS that what you want?
sori my bad that should be == not !=
And you forgot to return anything if value is not found.
i tried that but still get the sme output that search loop was given to me as a psuedo code
here is the search loop code in 2d vectors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
voidsearch(constMatrix& m, intx, int& i, int& j) {
// Pre:  m is a non-empty matrix
// Post: i and j define the location of a cell that 
// contains the value x in M. In case x is not in m, 
// then i = j = -1
intnrows = m.size();
intncols = m[0].size();
for(i = 0; i < nrows; ++i) {
for(j = 0; j < ncols; ++j) {
if(m[i][j] == x) return;
}
}
i = -1;
j = -1;
}
Last edited on
Topic archived. No new replies allowed.