Binary Search on an Array of Struct

This is a discussion on Binary Search on an Array of Struct within the C++ Programming forums, part of the General Programming Boards category; Hello.... I am having a hard time trying to make a record try and search an item code that a ...

  1. #1
    curwa1
    Guest

    Question Binary Search on an Array of Struct

    Hello....

    I am having a hard time trying to make a record try and search an item code that a user would enter and then match it with the record from the struct.
    Code:
    // c++ code:
    sturct salesrecord
           {
             char item_code[8];
             int quantity;
             float price;
           }
            salesrecord Sales;
            
           char code;
            int first;
            int last;
            int mid;
            .
            .   // blah blah blah
    
    //Here is what I wrote for the binary search that does not work...
    
           cin >> code;
      
            first = 0;
            last = max;
    
             // binary search
            while ( (first <= last) && (Sales[mid].item_code != code) )
            {    mid = (first + last / 2) ;
    
              if (Sales[mid].item_code < code)
              { //look in upper half
                first = mid + 1;
              }
              else
              { //look in lower half
                last = mid - 1;
              }
              mid = (first + last) / 2;
            }
          if (Sales[mid].item_code == code)   //match
          {
           ......
    I tried everything for it to search but it still does not want to work....can i get any help out there??? Thanks in advanced....


    Code tags added by Hammer

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Recommendations

    1. Make it an array of structs

    2. when using a binary search the data has to be sorted allready.


    3. Don't "reinvent the wheel" look at books and other sources for binary search code and modify it to fit your needs.
    Mr. C: Author and Instructor

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Recommendations
    ... and another, please use code tags (see my signature for help on this).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Curwa1
    Guest

    Tying again...

    /* This is what I wrote.. */
    Code:
    #include <string.h>     //strcmp
    #include <iomanip.h>
    #include <fstream.h>	//file stream and cin ,cout
    #include <iostream.h>	//setiosflags, setprecision
    
    const int max = 9;                    // 10 records in file  
    
    struct SalesRecord		// array of struct (which is sorted)
    {
        char item_code[8];                   //given
        int quantity_on_hand;
        float unit_selling_price;
    };
    
    void main (void)
    {
      SalesRecord Sales[max];
    
      ifstream ifsServ;   //Input file stream
      ofstream ofsServ;   //Ouput file stream
      char inputfile[30];
      char outputfile[30];
    
      cout << "Enter input path: " << endl;
      cin >> inputfile;
      cout << "Enter output path: "<<endl;
      cin >> outputfile;
    
      ifsServ.open(inputfile);   //Open the input file stream
      ofsServ.open(outputfile);  //Open the output file stream
      char code[8];
      int quantity;
      float value;
      
      int lowx = 0;        //index ranges
      int hix = max -1;
      int midx = 0;
      int i = 0;
      
      if (!ifsServ)
      {
       cout << "Error opening input file" <<endl;
      }
      else
      {
         if (!ofsServ)
         {
           cout << "Error opening output file" << endl;
         }
         else
         {
        //Read first line:
    
       ifsServ >> Sales[i].item_code[8]
                >> Sales[i].quantity_on_hand
                >> Sales[i].unit_selling_price;
         }
      }
        cout << "Enter Item Code: " ;
        cin >> code;
    
        while (strcmp (code,"quit") !=0)
        {
          cout << "Enter Quantity Order: " ;
          cin >> quantity;
    
                                                        // binary search (in the book)
            midx = (hix + lowx) /2;
            while ( (hix > lowx) && (Sales[midx].item_code != code))
            {
              if (Sales[midx].item_code < code)
              {
               lowx = midx+1;
              }
              else
              {
               hix = midx - 1;
              }
    
              midx = (hix + lowx) / 2;
            }
            if (Sales[midx].item_code == code)                 //if found
            {
            cout << "Found a MATCH!" << endl;
           }
            else
            {
            cout << "Item Code Not Recognized!" << endl; //if not found
             }
    
     cout << "Enter Item Code: " ;
     cin >> code;
    
     i++;
     ifsServ >> Sales[i].item_code[8]
        		>> Sales[i].quantity_on_hand
                >> Sales[i].unit_selling_price;
    }
    }
    /*
    It compiles but cannot find a match with the code entered by the user? For example enter code = ABC1110 and in the record there is a ABC1110 in item_code. Any specific help would be aprreciated! */


    &#91;code]&#91;/code]tagged by Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. binary search in an array
    By brianptodd in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2002, 01:05 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21