public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* simple binary search program and gdb, plz help
@ 2011-05-30 22:00 eric lin
  2011-05-31 12:20 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: eric lin @ 2011-05-30 22:00 UTC (permalink / raw)
  To: gcc-help

Dear gnu / g++ programers:

  I copy a piece of simple code from c++ book (practical c++ programing) , page 296, chapter Debugging and optimation
with input also according its indicated , increasing in order in numbers.dat (page 289)  file
---------------------
eric@eric-laptop:~/practicalCpp$ cat search0.cpp
/**********************************************************************************
 * search -- Search a set of numbers.                                             *
 *                                                                                *
 * Usage:                                                                         *
 *      search                                                                    *
 *             you will be asked numbers to lookup                                *
 *                                                                                *
 * Files:                                                                         *
 *     numbers.dat -- numbers 1 per line to search                                *
 *                     (Numbers must be ordered)                                  *
 **********************************************************************************/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <assert.h>

const int MAX_NUMBERS = 1000;        // Max numbers in file
const char *const DATA_FILE = "numbers.dat";       // File with numbers

int data[MAX_NUMBERS];            // Array of numbers to search
int max_count;                    // Number of valid elements indata

int main()
{
  std::ifstream in_file;      // Input file
  int middle;                 // Middle of our search range
  int low, high;              // Upper/lower bound
  int search;                 // number to search for

  in_file.open(DATA_FILE, std::ios::in);
  if (in_file.bad()) {
    std::cerr << "Error:Unable to open " << DATA_FILE << '\n';
    exit(8);
  }

  /*
   * Read in data
   */

  max_count = 0;
  while (true) {
    char line[30];    // Line from the input file

    if (in_file.eof())
      break;

    in_file.getline(line, sizeof(line));

    assert(max_count >= 0);
    assert(max_count < sizeof(data)/sizeof(data[0]));
    std::sscanf(line, "0", &data[max_count]);
    if (data[max_count] == -1)
      break;

    ++max_count;
  }

  while (true) {
    std::cout << "Enter number to search for or -1 to quit:";
    std::cin >> search;

    if (search == -1)
      break;
  
    low = 0;
    high = max_count;

    while (true) {

//////////
      if (low >= high) {
        std::cout << "Not found\n";
        break;
      }
///////////
      middle = (low + high) / 2;

      assert(middle >= 0);
      assert(middle < sizeof(data)/sizeof(data[0]));
      if (data[middle] == search) {
        std::cout << "Found at index " << middle << '\n';
        break;     
      }

/*
      if (low == high) {
        std::cout << "Not found\n";
        break;
      }
*/


      assert(middle >= 0);
      assert(middle < sizeof(data)/sizeof(data[0]));
      if (data[middle] < search)
        low = middle + 1;
      else
        high = middle - 1;
    }
   }
   return (0);
}
eric@eric-laptop:~/practicalCpp$ cat numbers.dat
4
6
14
16
17

eric@eric-laptop:~/practicalCpp$
--------------------------------------------------
but it not only fail to find any numbers in it, like 4, 6, 14,
and
if I input any character non-integer, it response unstop "Enter number to search for or -1 to quit:"

plz help, Eric/* all that chapter talk about gdb is almost not work on my test, for example , print middle
                     no symbol table is loaded.   Use the "file" command                                                             */

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: simple binary search program and gdb, plz help
  2011-05-30 22:00 simple binary search program and gdb, plz help eric lin
@ 2011-05-31 12:20 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2011-05-31 12:20 UTC (permalink / raw)
  To: eric lin; +Cc: gcc-help

"eric lin" <ericlin@fsshl.zzn.com> writes:

>   I copy a piece of simple code from c++ book (practical c++ programing) , page 296, chapter Debugging and optimation
> with input also according its indicated , increasing in order in numbers.dat (page 289)  file

This mailing list does not exist to help you learn how to program.
Sorry.

>     std::sscanf(line, "0", &data[max_count]);

This line looks wrong to me.

I recommend that you take basic debugging steps like adding some print
statements to verify that the array is what you expect, or run the
program under a debugger.

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-05-31  5:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-30 22:00 simple binary search program and gdb, plz help eric lin
2011-05-31 12:20 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).