Problem Solving and Program Development

How do I organize my thoughts about solving a problem?

If you can't solve the problem by hand, chances are you won't get the computer to do it for you either.

Consider the following example of writing a program that gives the average of students' midterm 1 grades:

More formally, the above steps of problem solving and implementation on a computer can be summarized as:

  1. Problem Specification: what's the input and output look like?
    • Input: 90 integers ranging from 0 to 100, e.g. 95 68 100 99 ...
    • Output: average of the input numbers, e.g. 90
  2. Algorithm Design: what are the processing steps used to transform the input into the output? E.g.
    1. sum = 0, counter = 1
    2. Read a grade
    3. counter = counter + 1
    4. sum = sum + grade
    5. if counter < 90 then go back to step 2
    6. mean = sum / 90
    7. display the mean
  3. Hand Test
  4. Coding
  5. Testing

Program Development: How do I go about writing a program?

  1. Use a program "template" as a starting point. It should contain a sample header documentation section.
  2. Next figure out exactly what your input and output is supposed to look like. Write your program to handle this input and output, even if the "answers" are all wrong. This will help you in subsequent stages of writing. Get your program to compile and run at this point with just the program "skeleton".
  3. Now add functionality to your program one piece at a time, getting each piece to compile and run, and verifying that it is correct up until that point. The following approaches/attitudes have been shown to end up taking much longer:
    • I'll just type the whole thing in, then try to get it to run.
    • I don't understand what's wrong, so I'll just randomly change things and hope I get lucky.
    • I don't understand how to do it by hand, but I'm sure the computer is smart enough to get it to work
  4. As you get each stage of your program to work, make a copy of it (in other words copy the file) before starting on the next stage. That way if you totally goof up, you don't have to start over.


[CS Dept] [UIC] [Prof. Reed]