C++ Functions And Arrays part 2

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?
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
38
39
40
41
42
43
44
45
#include <iostream> 
using namespace std; 
bool search(int arr[], int size, int val); 
const int 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
			return true;
return x;
	}
return false;
}
Last edited on
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.

I've added some comments near my changes.
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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream> 
using namespace std; 
bool search(int arr[], int size, int val); 
const int 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
	    return true;        // no need to set i or x here.
	}
    }
    return false;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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? ;)
			return true; // 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
return false; // 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 


Have fun
Don't return -1 from main(). If you want the result to be 255, return 255.
Topic archived. No new replies allowed.