C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
ID 891 was not found in list.
***Customer Balance Lookup***
What customer number do you need to check? 475
** That customer's balance is $192.41.
No additional credit

The first part of the program defines and initializes two arrays with the customer ID numbers and
matching balances. As you know, when you first define arrays, you can use the assignment operator,
=, to assign the array’s data.


After printing a title and asking for a customer ID number, the program uses a for loop to step
through the parallel arrays looking for the user’s entered customer ID. If it discovers the ID, a found
variable is set to true ( 1 ) for later use. Otherwise, found remains false ( 0 ).


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.

The program’s for loop might end without finding the customer. The code following the for loop
would have no way of knowing whether the for’s break triggered an early for loop exit (meaning
that the customer was found) or whether the for ended normally. Therefore, the found variable lets
the code following the for loop know whether the for found the customer.


When the for loop ends, the customer is found (or not found). If found, the following two conditions
are possible:



  • The balance is already too high.

  • The balance is okay for more credit.


No matter which condition is the true condition, the user is informed of the result. If the customer was
not found, the user is told that and the program ends.


How was that for a real-world program? Too difficult, you say? Look it over once or twice more.
You’ll see that the program performs the same steps (albeit in seemingly more detail) that you would
follow if you were scanning a list of customers by hand.


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).
Free download pdf