20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100 Chapter 7 Work<strong>in</strong>g with Arrays<br />

Program 7.1<br />

Output<br />

values[0] = 197<br />

values[1] = 0<br />

values[2] = -101<br />

values[3] = 547<br />

values[4] = 0<br />

values[5] = 350<br />

values[6] = 0<br />

values[7] = 0<br />

values[8] = 0<br />

values[9] = 35<br />

The variable <strong>in</strong>dex assumes the values 0 through 9 as the last valid subscript of an array<br />

is always one less than the number of elements (due to that zeroth element). Because you<br />

never assigned values to five of the elements <strong>in</strong> the array—elements 1, 4, and 6 through<br />

8—the values that are displayed for them are mean<strong>in</strong>gless. Even though the program’s<br />

output shows these values as zero, the value of any un<strong>in</strong>itialized variable or array element<br />

is not def<strong>in</strong>ed. For this reason, no assumption should ever be made as to the value of an<br />

un<strong>in</strong>itialized variable or array element.<br />

Us<strong>in</strong>g Array Elements as Counters<br />

It’s now time to consider a slightly more practical example. Suppose you took a telephone<br />

survey to discover how people felt about a particular television show and you<br />

asked each respondent to rate the show on a scale from 1 to 10, <strong>in</strong>clusive. After <strong>in</strong>terview<strong>in</strong>g<br />

5,000 people, you accumulated a list of 5,000 numbers. Now, you want to analyze<br />

the results. One of the first pieces of data you want to gather is a table show<strong>in</strong>g the<br />

distribution of the rat<strong>in</strong>gs. In other words, you want to know how many people rated<br />

the show a 1, how many rated it a 2, and so on up to 10.<br />

Although not an impossible chore, it would be a bit tedious to go through each<br />

response and manually count the number of responses <strong>in</strong> each rat<strong>in</strong>g category. In addition,<br />

if you have a response that could be answered <strong>in</strong> more than 10 ways (consider the<br />

task of categoriz<strong>in</strong>g the age of the respondent), this approach would be even more<br />

unreasonable. So, you want to develop a program to count the number of responses for<br />

each rat<strong>in</strong>g.The first impulse might be to set up 10 different counters, called perhaps<br />

rat<strong>in</strong>g_1 through rat<strong>in</strong>g_10, and then to <strong>in</strong>crement the appropriate counter each time<br />

the correspond<strong>in</strong>g rat<strong>in</strong>g was entered. But once aga<strong>in</strong>, if you are deal<strong>in</strong>g with more than<br />

10 possible choices, this approach could become a bit tedious. And besides, an approach<br />

that uses an array provides the vehicle for implement<strong>in</strong>g a much cleaner solution, even <strong>in</strong><br />

this case.<br />

You can set up an array of counters called rat<strong>in</strong>gCounters, for example, and then<br />

you can <strong>in</strong>crement the correspond<strong>in</strong>g counter as each response is entered.To conserve<br />

space <strong>in</strong> this book, Program 7.2 assumes you are deal<strong>in</strong>g with only 20 responses. In addition,<br />

it’s always good practice to first get a program work<strong>in</strong>g on a smaller test case before

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!