-
data structure
the problem is:
write the sequential search method that find the last occurance of an item
the code runs but it donot give the right answer
here is my code
Code:
#include<iostream>
using namespace std;
int seqsearch(int seq[],int size,int key)
{
int i;
for(i=size;i>0;i--)
{
if(seq[i]==key)
return i;
return -1;
}
}
int main()
{
int i;
int key;
int seq[10]={0,1,2,3,4,5,6,7,8,9};
int x=seqsearch(seq,10,key);
cout<<"enter the key"<<endl;
cin>>key;
cout<<x<<endl;
return 0;
}
-
In main pay attention to the order of your calls, especially what value will key have when you pass it on to the search function. Also in the search function pay attention to the start-value of i and what the lower bount should be (remember that arrays are indexed from 0 to size-1, not from 1 to size).
On an unrelated note you need to start paying attention to indention, or you will very soon find your code to be a complete mess to read.