View Single Post
Old 18-02-2008, 11:34 PM   #411 (permalink)
mavihs
Techie By Heart
 
mavihs's Avatar
 
Join Date: Feb 2007
Location: Cyber City
Posts: 769
Post Re: Post ur C/C++ Programs Here

Code:
/* WAP to execute the following function:
    -> Read an integer & an integer array & search for that integer in the intger array & return its position, incase the number is not there in the list, function should return -1.*/

int search(int siz, int ele, int arr[]);
void main()
{
    clrscr();
    int arr[25], siz, i, ele, j;
    cout<<"Enter the number of elements:";
    cin>>siz;
    cout<<"Enter the array:";
    for(i=0; i<siz; i++)
    {
        cin>>arr[i];
        cout<<"\t";
    }
    cout<<"Enter the element to be searched";
    cin>>ele;
    k=search(siz, ele, arr);
    if(j==-1)
        cout<<"Element not found";
    else
        cout<<"The element found at"<<j<<"th position";
        getch();
}
int search(int siz, int ele, int arr[])
{
    int pos=-1;
    for(int i=0; i<siz; i++)
    {
        if(ele==arr[i])
        {
            pos=i;
            break;
        }
        else
        pos=2;
    }
    if(pos==1)
    {
        return(i+1);
    }
    else
    {
        return(-1);
    }
}
__________________
█§ ║╖ î v â ╥╥╖█(™©)
mavihs is offline