C Tutorial (22) : Searching

You won’t become an ace programmer if you aren’t exposed a bit to searching and sorting values. Complete books have been written on searching and sorting techniques, and the next two tutorials present only the simplest techniques.

Filling Arrays

Some arrays, such as the day counts in each of the 12 months are known in advance. You might initialize arrays with such values when you define the arrays or when you use assignment statements.

You will also be filling arrays with values that your program’s users enter.

Other data values come from disk files. Customer records, inventory values, and school transcript information is just too voluminous for users to enter each time a program is run.

Your programs can and will fill arrays using a combination of all three of these methods:

  • Assignment
  • User data entry
  • Disk files

Finders, Keepers

Think about the following scenario: Your program contains an array that holds customer ID numbers and an array that holds the same number of customer balances. Such arrays are often called parallel arrays because the arrays are in synch—that is, element number 14 in the customer ID array contains the customer number that owes a balance found in element 14 of the balance array.

The customer balance program might fill the two arrays from disk data when the program first starts. As a customer places a new order, it’s your program’s job to find that customer balance and stop the order if the customer owes more than $100 already (the deadbeat!).

/* This program takes an ID number from the user and then checks the
   ID against a list of customers in the database. If the customer
   exists, it uses that array element to check their current
   balance, and warns the user if the balance is more than 100 */

#include <stdio.h>

int ctr; // Loop counter
int idSearch; // Customer to look for (the key)
int found = 0; // Will be 1 (true) if customer is found

// Defines the 10 elements in the two parallel arrays
int custID[10] = {313, 453, 502, 101, 892, 475, 792, 912, 343, 633};
float custBal[10] = {0.00, 45.43, 71.23, 301.56, 9.08,
                     192.41, 389.00, 229.67, 18.31, 59.54};

/* Interact with the user looking for a balance. */ 
printf("\n\n*** Customer Balance Lookup ***\n");

printf("What customer number do you need to check? "); 
scanf(" %d", &idSearch);

/* Search to see that the customer ID exists in the array */ 
for (ctr=0; ctr<10; ctr++)
 if (idSearch == custID[ctr])
  found = 1;

if (found)
 if (custBal[ctr] > 100.00)
  printf("\n** That customer's balance is $%.2f.\n", custBal[ctr]);
  printf(" No additional credit.\n");
   printf("\n** The customer's credit is good!\n");
  printf("** You must have typed an incorrect customer ID.");   
  printf("\n ID %3d was not found in list.\n", idSearch);

Tip : The found variable is often called a flag variable because it flags (signals) to the rest of the program whether the customer ID was or was not found.

Note : What’s really important is that if there were a thousand, or even 10,000 customers, and the arrays were initialized from a disk file, the same search code would work! The amount of data doesn’t affect the logic of this program (only the way the arrays are initialized with data).

Example program use a sequential search because the arrays (customer ID) are searched from beginning to end until a match is found. You’ll learn about more advanced searches as your programming skills improve. In the next tutorial, you’ll see how sorting an array helps speed some array searches.

You saw how to find array values based on a key. The key is a value that the user enters. Until you learn more about searches, use a sequential search because it is the easiest search technique to master. Don’t forget that a match might not be found. Always assume that your search value might not be in the list of values and include the code needed to handle an unfound value.

Array < Prev                                                                                       Next > Sorting



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s