Switch to full style
For C/C++ coders discussions and solutions
Post a reply

c++/data structure

Wed Oct 27, 2010 11:37 pm

my problem :is write a sequential search method that finds the last occurrance of an item
the program runs but it doesnot give me the right answer
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[]={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;
  }





Re: c++/data structure

Sat Oct 30, 2010 12:16 am

fix like this
Code:

for(i=size;i>=0;i--)
{
if(seq[i]==key)
return i ;


} return -1 ;


and
Code:
int main()

  {
     int i;
     
     
      int key;
      int seq[]={0,1,2,3,4,5,6,7,8,9};
     

   cout<<"enter the key"<<endl;
   cin>>key;
// Call the function after the entry of key
int x=seqsearch(seq,10,key);
   cout<<x<<endl;
   

  return 0;
  }



Post a reply
  Related Posts  to : c++/data structure
 Display data from database as Tree Structure in JSP     -  
 What is the size of this Structure     -  
 ASP file structure     -  
 program using structure to store roll number,name     -  
 What is Data integration?     -  
 Data set for ID3 algorithm     -  
 Delete data from database by ID in php     -  
 session Encoding Data in php     -  
 Is String a data type     -  
 Only the data needs to be updated and not the whole JSP page     -  

Topic Tags

C++ Algorithms