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

(Romina) #1

22. Searching Arrays


In This Chapter


  • Filling arrays

  • Searching parallel arrays for specific values


You bought this book to learn C as painlessly as possible—and that’s what has been happening. (You
knew that something was happening, right?) Nevertheless, 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 chapters present only the simplest techniques. Be
forewarned, however, that before you’re done, this chapter and the next one might raise more
questions than they answer.


You’ll find that this and the next chapter are a little different from a lot of the others. Instead of
teaching you new C features, these chapters demonstrate the use of C language elements you’ve been
learning throughout this book. These chapters focus on arrays. You will see applications of the array
concepts you learned in Chapter 21, “Dealing with Arrays.” After these chapters strengthen your array
understanding, Chapter 25, “Arrays and Pointers,” explains a C alternative to arrays that sometimes
comes in handy.


Filling Arrays


As Chapter 21 mentioned, your programs use several means to fill arrays. Some arrays, such as the
day counts in each of the 12 months, historical temperature readings, and last year’s sales records, 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. A customer order-
fulfillment program, for example, gets its data only as customers place orders. Likewise, a scientific
lab knows test values only after the scientists gather their results.


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.


In reality, your programs can and will fill arrays using a combination of all three of these methods:



  • Assignment

  • User data entry

  • Disk files


This book has to keep the programs simple. Until you learn about disk files, you’ll see arrays filled
with assignment statements and possibly simple user data entry (and you’ll be the user!).


Note
Free download pdf