Total members 11889 |It is currently Thu Mar 28, 2024 8:00 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;
  }






Author:
Newbie
User avatar Posts: 2
Have thanks: 1 time

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;
  }



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : c++/data structure
 Display data from database as Tree Structure in JSP     -  
 ASP file structure     -  
 What is the size of this Structure     -  
 program using structure to store roll number,name     -  
 What is Data integration?     -  
 Data set for ID3 algorithm     -  
 range of data type     -  
 Arithmetic data comparison in C#     -  
 java data types     -  
 Delete data from database by ID in php     -  



Topic Tags

C++ Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com