Mega Code Archive

 
Categories / C / File
 

Search a set of numbers

/* Practical C Programming, Third Edition By Steve Oualline Third Edition August 1997  ISBN: 1-56592-306-5 Publisher: O'Reilly */ #include <stdio.h> #define MAX_NUMBERS   1000    /* Max numbers in file */ const char DATA_FILE[] = "numbers.dat";  /* File with numbers */ int data[MAX_NUMBERS];  /* Array of numbers to search */ int max_count;    /* Number of valid elements in data */ int main() {     FILE *in_file;  /* Input file */     int  middle;    /* Middle of our search range */     int low, high;  /* Upper/lower bound */     int search;    /* number to search for */     char line[80];  /* Input line */     in_file = fopen(DATA_FILE, "r");     if (in_file == NULL) {   fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE);   exit (8);     }     /*      * Read in data       */     max_count = 0;     while (1) {   if (fgets(line, sizeof(line),  in_file) == NULL)       break;   /* convert number */   sscanf(line, "%d", &data[max_count]);   ++max_count;     }     while (1) {   printf("Enter number to search for or -1 to quit:" );   fgets(line, sizeof(line), stdin);   sscanf(line, "%d", &search);   if (search == -1)       break;   low = 0;   high = max_count;   while (1) {       if (low >= high) {     printf("Not found\n");     break;       }       middle = (low + high) / 2;       if (data[middle] == search) {     printf("Found at index %d\n", middle);     break;       }       if (data[middle] < search)     low = middle +1;       else     high = middle -1;   }    }    return (0); } //File numbers.dat /* 4 6 14 16 17 18 22 23 26 27 28 29 34 37 40 41 42 46 47 48 51 53 55 57 61 68 69 72 73 74 75 76 77 78 79 82 87 92 93 95 96 99 100 106 107 109 112 113 114 118 120 123 124 125 127 132 135 140 142 145 146 148 150 153 155 158 159 162 165 166 167 172 179 180 183 185 187 191 193 195 196 197 199 201 202 203 209 210 220 222 224 226 228 229 232 234 239 240 242 243 244 246 247 248 250 252 253 254 255 261 264 272 276 279 280 284 288 291 295 296 297 298 304 306 309 310 314 322 325 326 327 329 333 334 341 345 346 349 355 358 360 361 362 363 364 371 372 379 380 382 385 386 387 389 390 391 398 401 404 405 407 409 411 412 421 422 423 428 431 433 435 436 437 438 439 442 444 445 447 448 449 450 451 452 454 456 465 466 467 470 471 472 475 476 478 481 482 484 485 487 490 491 498 501 503 507 509 510 511 512 513 514 517 520 521 523 525 527 528 529 530 535 539 542 548 549 555 556 557 558 559 560 561 568 571 572 574 587 589 590 592 593 598 601 603 604 605 608 609 610 613 614 615 616 619 620 624 628 631 633 636 638 640 643 645 647 648 650 655 656 658 666 667 668 669 670 674 675 678 680 681 689 691 692 693 694 695 697 701 702 704 707 708 710 711 713 714 715 716 718 722 723 726 730 732 736 739 745 746 747 750 753 755 756 757 760 762 763 765 766 769 772 773 776 777 779 781 784 790 794 798 800 808 812 813 815 818 822 823 826 827 830 832 837 838 839 840 842 844 845 847 848 851 853 857 858 859 862 864 874 879 882 883 890 892 895 905 906 907 912 914 918 922 924 927 930 931 938 939 943 945 946 948 949 950 955 958 960 964 966 971 972 976 977 978 979 980 984 986 987 988 990 991 992 */