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:

    • The program takes as input the grades for the 90 students. Each grade is a whole number (an integer) from 0 to 100. The results of running the program, the output, is the average (the mean) of the numbers

    • What the program will need to do is to step through all the scores, accumulating the sum and keeping track of how many scores have been seen. The average will be calculated as the sum divided by the total number of scores.

    • After figuring out the process described above, it is a good idea to test out this process by hand on a small problem.

    • If the process seems to work correctly for a small problem, then the next step is to translate the process into a computer language such as C and type it in.

    • After the program compiles and runs correctly, test it, paying careful attention to the "boundary conditions", such as when there are 0 students, or only 1 student, or more than 90 students. Also consider whether or not you need to do error checking for your input, such as verifying that each score is in fact within the 0..100 range.

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

    1. Problem Specification: what do 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

    1. Hand Test

    2. Coding

    3. 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.