From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30552 invoked by alias); 30 May 2011 18:47:21 -0000 Received: (qmail 30543 invoked by uid 22791); 30 May 2011 18:47:21 -0000 X-SWARE-Spam-Status: No, hits=2.1 required=5.0 tests=AWL,BAYES_40,MISSING_MIMEOLE X-Spam-Check-By: sourceware.org Received: from mailout1.mailcentro.com (HELO c2mailgw101.mailcentro.net) (208.67.179.151) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 30 May 2011 18:47:05 +0000 Received: from c2web212 (wnat.mailcentro.com [208.67.179.158]) by c2mailgw101.mailcentro.net (8.13.8(MC-AXH/MJD-001)/8.13.8.axh-mjd) with SMTP id p4UIl2XT013334; Mon, 30 May 2011 11:47:02 -0700 X-MC-Sender: ericlin@fsshl.zzn.com X-MC-IPAddr: wnat.mailcentro.com [208.67.179.158] X-Version: Mailcentro(english) X-SenderIP: 99.116.255.96 X-SenderID: 12592607 X-RealDate: 5/30/2011 11:47:04 AM From: "eric lin" Message-Id: Date: Mon, 30 May 2011 22:00:00 -0000 Content-Type: text/plain; charset=iso-8859-1 To: gcc-help@gcc.gnu.org Subject: simple binary search program and gdb, plz help Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-MC-Filter: 7.6.5 X-MC-Filter-AntiVirus: Scanned for virus - Status 0 X-MC-FilterSkip: 0 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-05/txt/msg00429.txt.bz2 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 #include #include #include #include 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 */