2D Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>					
using namespace std;
void main()
{
	int a;
	int arr[5][5]={{5,2,7,9,10},{4,7,10,11,13},{5,8,3,2,9},{83,57,3,7,14},{1,8,17,34,60}};
	cout<<"Enter a number\n";
	cin>>a;
	for(int i=0;i<5;i++)
		for(int j=0;j<5;j++)
		{
			if(a==arr[i][j])
			{
				cout<<"Number found\n";
			}
			else
			{
				cout<<"Number Not Found\n";
			}
		}
	system("pause");
}


this is my code. i want to check the input number is in the 2d array or not
what the logic should i used in this program.
in my code compiler checks all the numbers in array and prints all tha statements one by one i need answer only yes if the input number exists in the array or only no if the number does not exists in the array.
Last edited on
Here is the solution:
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
#include<iostream>
using namespace std;
int main()  //You cannot set return type of main to void
//My compiler gave me an error.
{
	int a;
	int arr[5][5]={{5,2,7,9,10},{4,7,10,11,13},{5,8,3,2,9},{83,57,3,7,14},{1,8,17,34,60}};
	bool isFound = 0;
	cout<<"Enter a number :\n";
	cin>>a;
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			if(a==arr[i][j])
			{
				isFound = 1;
				break;
			}
			else
			{
				isFound = 0;
			}
		}
		if(isFound)
		{
			cout << "Number is found.\n";
			break;
		}
	}

	if(!isFound)
		cout << "Number is not found.\n";

	system("pause");
	return 0;
}
i want it without using bool statement
@Mohamed Fouah That is one way of doing it but you don't have to set isFound every time you perform the conditional (if) check. It will remain false unless set to true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isFound = false;
cout<<"Enter a number :\n";
cin>>a;
for(int i=0;i<5;i++)
{
	for(int j=0;j<5;j++)
	{
		if(a==arr[i][j])
		{
			isFound = true;
                        cout << "Number is found.\n";
			break;
		}			
	}
       if(isFound)
           break;
}

if(!isFound)
	cout << "Number is not found.\n";
@ OP: Why would you not want bool statements? I'm curious.
i dont understand bool statement.i dont read it in college
i want this only using if else not istrue.i dont understand it
Sorry, that's not a valid answer especially for something like bool. On the bright side this would be a good piece to learn bool statements from.
Boolean statements are important!
what is my fault in this.my teachers dont teaches bool statement .may be they will teach bool statement in future.
It's not your fault, it is however your problem. No one here is mad at you we can all sympathise with having a teacher who skips over important parts of the text book. But you cannot let that one person get in your way of learning these things.
Its not your fault. I'm sure your teachers will be impressed if you use them here. If they explicitly tell you that you can't use them. You can always just use a integer variable and just either allocate it a 0 (false) or 1 (true). I recommend sticking to boolean though.
Last edited on
yes you are right but my teacher will ask me that tell me the information about bool then what i do
You read this and find out what it is:
http://www.cplusplus.com/doc/boolean/
ok thanks.but there is any way to solve my problem without bool.?
I told you above. You can use an int value instead of a boolean.

If they explicitly tell you that you can't use them. You can always just use a integer variable and just either allocate it a 0 (false) or 1 (true).
Last edited on
Well, sure, you can use an int as GodPyro said, but the bool really is the way to go.

-Albatross
Last edited on
@omeraslam if this satisfies your question please mark this thread as solved!
yes i m satisfy.thank you all
Topic archived. No new replies allowed.