How could i perform searching in 2d array.

How could i perform searching in 2d array.For eg 2d array is
int arr[2][4]={(23 34 45 5),(22 44 55 54)}

how could i search 43 from array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{

     int arr[2][4] = {(23, 34, 45, 5), (22, 44, 55, 54)};
     bool found = false;

     for (int i = 2;i < 2;i++)
     {
           for (int j = 0;j < 4;j++)
           {
                 if (arr[i][j] == 43)
                 {
                        found = true;
                 }
           }

     }
}


My syntax might be wrong, but that's how you can search an array
zrrz, your entire 'for' loop is incorrect. Line 7 will not run at all, because you initialize i at 2, and then the condition is i<2, which is false. Also, you don't use the main advantage of bool, which is to stop the loop as soon as found becomes true.. In the end, you still won't know where 43 is, even though you know it is there...

jahanzaib ali khan Iiu, you're not allowed to post the same problem twice. You could have very well posted this doubt in your previous post as well.
http://www.cplusplus.com/forum/beginner/59324/

Anyways, basically you have to do what xrrz said... Something like:
1
2
3
4
5
6
7
8
9
10
11
12
bool found=false;

for (int i=0; (i<2)&&(found==false); i++)
{
	for (int j=0; (j<4)&&(found==false); j++)
	{
		if (arr[i][j]==43)
			found=true;
	}
}

cout<<"43 is at ("<<i<<", "<<j<<")";   //Prints location of 43. 


Hope this helps.. :)
Oh man, what the heck was I thinking? Haha, i don't know why i made it initialize at 2 :) I should probably proofread stuff.
Caprico, i am sorry i am new into this forum and i thought that posting twice could help me more fastly so i did that.Anyhow Thanks a lot to both Caprico and zrrz for giving me the help........
Last edited on
Topic archived. No new replies allowed.