2D Array

May 1, 2011 at 1:30pm
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 May 1, 2011 at 1:35pm
May 1, 2011 at 1:47pm
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;
}
May 1, 2011 at 6:27pm
i want it without using bool statement
May 1, 2011 at 6:31pm
@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";
May 1, 2011 at 6:35pm
@ OP: Why would you not want bool statements? I'm curious.
May 1, 2011 at 6:59pm
i dont understand bool statement.i dont read it in college
May 1, 2011 at 7:02pm
i want this only using if else not istrue.i dont understand it
May 1, 2011 at 7:05pm
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.
May 1, 2011 at 7:09pm
Boolean statements are important!
May 1, 2011 at 8:04pm
what is my fault in this.my teachers dont teaches bool statement .may be they will teach bool statement in future.
May 1, 2011 at 8:09pm
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.
May 1, 2011 at 8:09pm
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 May 1, 2011 at 8:10pm
May 1, 2011 at 8:32pm
yes you are right but my teacher will ask me that tell me the information about bool then what i do
May 1, 2011 at 8:55pm
You read this and find out what it is:
http://www.cplusplus.com/doc/boolean/
May 1, 2011 at 9:01pm
ok thanks.but there is any way to solve my problem without bool.?
May 1, 2011 at 9:07pm
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 May 1, 2011 at 9:08pm
May 1, 2011 at 9:16pm
Well, sure, you can use an int as GodPyro said, but the bool really is the way to go.

-Albatross
Last edited on May 1, 2011 at 9:16pm
May 3, 2011 at 5:35am
@omeraslam if this satisfies your question please mark this thread as solved!
May 3, 2011 at 7:13am
yes i m satisfy.thank you all
Topic archived. No new replies allowed.