Need help on search algorithm!

Create a program that already has 5 integers assigned by use. Now, as program runs it will ask user to enter integer, if ineteger is already stored in array it says "you win and your integer was in location X" else "you lose".

I have to define my own nonvoid function.
do you want "help", or want someone to do it for you? The former is most preferred over the latter.
Here is my function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int i=0;
	int a[5]={63,77,86,34,19};
	
		cout<<"Enter an intger: ";
		cin>>i;
		if (i==a[5])
			{
				cout<<"You win!";
		}
		else
			{
				cout<<"You lose!";
		}
	
	return 0;
}
anyone please?
your function could return a Boolean (yes = won, false = not found)
iterate over the array and test each element with the user input.
And beware this:
if (i==a[5])
Is accessing the array out of bounds. And is only trying to check one value.

Okay!

I am done with this thing, but I have to create a non void function for that.
How would I go for it?

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
#include<iostream>
using namespace std;

int main()
{
    int a[5];
    int elements, key;
    bool found = false;
    cout<<"Enter the number of element: ";
    cin>>elements;
    for(int i = 0; i < elements; i++){
        cout<<"array["<<i<<"]: ";
        cin>>a[i];
    }
    cout<<"Enter the value to search: ";
    cin>>key;
    for(int i=0;i<elements;i++){
        if(key == a[i]){
            found = true;
            cout<<"The value is found at index array["<<i<<"]";
        }
    }
    if(!found){
        cout<<"Key not found!";
    }
    return 0;
}
I've already told you a hint about what your function should return.
and that code you've just posted doesn't fulfil your project requirements.

cout<<"Enter the number of element: ";

you're not supposed to ask the user for that..
My instructor told me that program should ask the user 5 integers first and then ask to search for one.

Like this (I made few 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
#include<iostream>
using namespace std;

int main()
{
    int a[5];
    int f;
    bool found = false;
    
    for(int i = 0; i < 5; i++)
	{
        cout<<"Enter an integer: ";
		cin>>a[i];
		
    }
    cout<<"Enter the value to search: ";
    cin>>f;
    for(int i=0;i<5;i++){
        if(f == a[i]){
            found = true;
            cout<<"The value is found at index array["<<i<<"]";
        }
    }
    if(!found){
        cout<<"Key not found!";
    }
    return 0;
}
I have to define my own nonvoid function.

I don't see any function other than main(), did you perhaps forget to write your required function?
Why not you post your question, exactly, so we can advice you on the right track, most of the time I might be wanting this to happen, but people helping me might be giving me an advice on that to happen.
This is getting really damn hard for 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// finalss.cpp : Defines the entry point for the console application.
//

#include<iostream>
using namespace std;

int ssrch(int a[], int k);

int main()
{
    int arr[99];
    int e, k;
	char t;
    bool found = false;
    cout<<"Enter the number of integers: ";
    cin>>e;
    for(int i = 0; i < e; i++)
	{
        cout<<"Enter an integer: ";
        cin>>arr[i];
    }
    cout<<"\nEnter the value to search: ";
    cin>>k;
    for(int i=0;i<e;i++)
	{
        if(k == arr[i])
		{
            found = true;
            cout<<"You Win - your number was in location["<<i<<"]"<<endl;
        }
    }
	if(!found)
	{
		cout<<"You lose, want to try again?: ";
	}
	
	if (t=='y')
	{
		ssrch(a[i],k);
	}
	else
	{
		cout<<"Exit!";
	}
    return 0;
}

	int ssrch(int a[], int k)
{
      int i=0;   
      while(i< array.length( )) && (k != array[i]))
      {
             
            if (array[index] != key)
                     index++;
      }
      return (index); 
} 
} 
Create a program that already has 5 integers assigned
My instructor told me that program should ask the user 5 integers first


Now i'm confused :)
Topic archived. No new replies allowed.