Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
using namespace std;
int array1[5];
int i, search;
bool found= false;
void main ()
{
    cout<<"Enter the five elements of an integer array \n";
    for (i=0 ; i<5 ; i++)
    {
        cin>>array1[i];
    }
    cout<<endl<<"Your integer array is:"<<endl;
    for (i=0 ; i<5 ; i++)
    {
        cout<<array1[i];
        cout<<"\t";
    }
    cout<<endl<<endl;
    cout<<"Enter a number to search for: \n";
    cin>>search;
    
    for (i=0 ; i<5 ; i++)
    {
        if (search==array1[i])
        {
            cout << "The number " << search << " was found and its index is " << i << endl;
            found= true;
            break;
        }
     }
     if (!found)
         cout << "The number " << search << " was not found!! " << endl;
}


Thanks for anyone's help.
Posted
Updated 26-Apr-10 14:17pm
v5

Hi, Grag.
It seems an school homework and here is the answer:

int find( int *pArray, int nLength, int nStart, int nTarget )
{
if ( nStart == nLength )
return -1;

if ( nTarget == pArray[nStart] )
return nStart;
else
return find( pArray, nLength, nStart++, nTarget );
}

Hope it will help you:)
Good luck!
 
Share this answer
 
I'll give you the general logic, you should try implementing it yourself:

enter the function with an array of 5 integers; If you've found your target, print your result, and exit the function. If it's not the right one, call the same function from within the function, but with an array of 4 integers (hint: ++array1).
When you've reached the end of array, exit anyway...
 
Share this answer
 
This smells like homework so I'll give you a plain English description instead of coding it up for you.

Add a function that takes in two integers (index and search) and returns a boolean. First the function should check the index, if it's greater than or equal to the size of the array then return false (meaning the item is not found). Otherwise it should compare the array item at the index specified to the search value. If the value is equal then the function can return true, otherwise it should return the result of calling itself and passing the value (current index + 1) as the index parameter. To start the search call the function from main with a parameter of 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900