Thread: data structure

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

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

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.
    Last edited by Shakti; 10-27-2010 at 04:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM