< previous page page_793 next page >

Page 793
0793-01.gif
Figure 14-10
Absence Records, Including Averages, by Department
We should make one additional point here. The best structure is the simplest one that accurately reflects the problem and the processing. For example, don't use either an array or a struct if simple variables suffice.
When you are deciding whether or not to use a structured data type, ask yourself, Can I process as I read, or must all the data be read in before I can begin processing? For example, if you are finding the average of a set of test grades, each test grade can be added into the sum as it is read. All you need are simple variablesno structured data types are necessary.
What if you want to compare each grade to the average? Because the average cannot be calculated until all grades have been read, each individual grade must be kept in memory. Therefore, the test grades should be stored in an array.
This discussion presupposes that you know where to begin. What if you look at a problem and don't even know what the choices are? Go back and carefully examine the problem statement. Do you understand what is being asked? Can you do by hand what is being asked? If so, what sorts of forms would you use? Would you set up a table with rows and columns on a sheet of paper? Would you set up a column and make hash marks? More than likely, the appropriate data structure resembles the forms you would create to do the job by hand.
If you cannot do the job by hand, your problem is more fundamental than the choice of a data structureyou need to clarify the problem. Try writing down everything you know about the problem. Then write down what your output must be and what you must have as input in order to produce that output. If necessary, refer to the problem-solving heuristics in Chapter 2.

 
< previous page page_793 next page >