Okay I'm trying to make the program return the index at which the first occurrence of the passed integer value was found, and -1, otherwise. Everything I've tried isn't working, can someone help or hint it to me?
#include <iostream>
usingnamespace std;
bool search(int arr[], int size, int val);
constint SIZE =25;
int main()
{
int nums[SIZE]; // array declaration
bool found;
int n;
//initialize array nums
for (int i = 0; i < SIZE; i++)
{
nums[i] = rand( ) % 251;
}
// display the content of array nums
cout << "\n************************\n";
for (int i = 0; i < SIZE; i++)
{
cout << nums[i] << "\t";
}
cout << "\n************************\n";
cout << "please enter a number between 0 to 250" << endl;
cin >> n;
found = search(nums, SIZE, n);
if (found)
cout << n << " was found in our data set!\n";
else
cout << n << " was NOT found in our data set!\n";
return 0;
}
bool search (int nums[], int SIZE, int n)
{
int x = 0;
for (int i = 0; i < SIZE; i++)
{
if (nums[i] == n)
i = x
returntrue;
return x;
}
returnfalse;
}
You're very close. Here is a cleaned up version that works, except that I'm running on Unix and the value returned to the environment from the program is 0-255. I'm returning -1, but that turns into 255 by the time you check the result.
#include <iostream>
usingnamespace std;
bool search(int arr[], int size, int val);
constint SIZE =25;
int main()
{
int nums[SIZE]; // array declaration
bool found;
int n;
//initialize array nums
for (int i = 0; i < SIZE; i++)
{
nums[i] = rand( ) % 251;
}
// display the content of array nums
cout << "\n************************\n";
for (int i = 0; i < SIZE; i++)
{
cout << nums[i] << "\t";
}
cout << "\n************************\n";
cout << "please enter a number between 0 to 250" << endl;
cin >> n;
found = search(nums, SIZE, n);
if (found) { // Note the braces
cout << n << " was found in our data set!\n";
return 0; // return 0 on success
} else { // more braces
cout << n << " was NOT found in our data set!\n";
return -1; // return -1 on failure.
}
}
bool search (int nums[], int SIZE, int n)
{
// int x = 0; You don't need x
for (int i = 0; i < SIZE; i++) {
if (nums[i] == n) { // Note the braces
returntrue; // no need to set i or x here.
}
}
returnfalse;
}
bool search (int nums[], int SIZE, int n); // you set the return type to bool
// but you wanted to return the position of the first occurrence or -1 if n wasn't found
=>
int search(int nums[], int SIZE, int n);
//let's have a look at your algorithm
{
int x = 0; // since i is the index anyway you don't need this one
for (int i = 0; i < SIZE; i++)
{
if (nums[i] == n) // now if we found n in our array
i = x // don't do this instead:
return i; // i has the position of n in our array, doesn't it? ;)
returntrue; // remove this
return x; // remove this
}
return -1; // If we haven't found the number n we will eventually get here and return -1
// if we don't get here we found n and returned the position above
returnfalse; // remove this
}
then
1 2 3 4 5 6
found = search(nums, SIZE, n); //change found to an int-variable
// then just make an if this way:
if(found != -1)
// yay we found n
else
//wasn't there