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

(Romina) #1
}
// Once the loop has completed, the ID was either found
// (found = 1) or not
if (found)
{
if (custBal[ctr] > 100)
{
printf("\n** That customer's balance is $%.2f.\n",
custBal[ctr]);
printf("No additional credit.\n");
}
else // Balance is less than $100.00
{
printf("\n**The customer's credit is good!\n");
}
}
else // The ID was not found
{
printf("** You have entered an incorrect customer ID.");
printf("\n ID %3d was not found in the list.\n", idSearch);
}
return(0);
}

Note

Other than the Draw Poker game in Appendix B, “The Draw Poker Program,” the
preceding program is this book’s hardest to understand. Mastering this program puts
you at a level above that of absolute beginner. Congratulations, and hats off to you
when you master the logic presented here. See, programming in C isn’t difficult after
all!

Before seeing this program, you mastered both array searching and array sorting. This program simply
puts the two procedures together. About the only additional job this program does is keep the two
parallel arrays in synch during the search. As you can see from the body of the search code, when
customer ID elements are swapped (within the custID array), the corresponding (via the subscript)
element in the customer balance array is searched.


An early search termination could take place because of the following:


Click here to view code image


if (custID[ctr] > idSearch) // No need to keep searching
{
break;
}

When there are several thousand array elements, such an if saves a lot of processing time.

Free download pdf