0

Hello,

I am working on an algorithm that checks to see if a value is in an array, however, it doesn't seem to want to work .. It will display that the first number is there but nothing else.. Any ideas? Heres the code:

#include <iostream>
using namespace std;
bool checkNumber(int arr[], int value)
{
	for(int i=0; (i < 10); i++)
	{
		if(value == arr[i])
		{
			return true;	
		}else{
			
			return false;
		}
	}	
}
int main() {
	
	int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	
	int value = 2;

	if(checkNumber(numbers, value))
	{
		cout << "Yes";
	
	}else{
		
		cout << "No";
	}
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by gerard4143
1

Take a look at your braces in your if statement.

Try this

bool checkNumber(int arr[], int value)
{
	for(int i=0; (i < 10); i++)
	{
	  std::cout << arr[i] << std::endl;
		if(value == arr[i])
		{
			return true;	
		}
	}	
	return false;
}

Edited by gerard4143: n/a

Votes + Comments
Thank you
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.