Program Design


Program design is the act of deciding how your code should best be structured.

Your overall goals are to produce a program that

Can we follow some systematic procedure to design programs?

Yes... at least for now while our programs are small.

We call this approach "step-wise refinement".

It is an example of a classic problem solving paradigm called divide and conquer.

If a problem looks too large to solve directly (the solution seems unclear or murky), then carve the problem into several smaller problems such that if you magically had the solutions to the smaller problems, then you can see how to combine those solutions to solve the larger problem.

An Example

Consider the specification for the program you need to write for Assignment 3. Here is the high-level algorithm we designed in class. In class, we expressed it as flow diagrams. Here, it is expressed as a JavaScript script containing only comments.

I have used indenting to indicate where the comments are a refinement of a higher-level activity.

First refinement of the algorithm

// create an array to hold the dictionary words
// this array should be of size 10

// get the user to load the dictionary array with 10 words

// ask the user to give a word to be looked up in the dictionary
// continue doing look ups until the user is tired of doing it

// generate the final report of information
// we need to show the dictionary, and the total number of times
// each word was looked up
// we also need to compute and show the average number of lookups per word.

Second refinement of the algorithm

// create an array to hold the dictionary words
// this array should be of size 10
   // this is small enough to put straight into JS code

// get the user to load the dictionary array with 10 words
   // use a for loop, 0 to maxWords
      // prompt for a word
      // assign the word to the next open dictionary location
  

// ask the user to give a word to be looked up in the dictionary
// continue doing look ups until the user is tired of doing it
   // see if the user want to look up a word
      // if no then end this loop and go on
      // if yes then 
         // prompt the user for a word
         // check to see if it is in the array
         // dont forget to count the hit if the word is found
         // alert the user as to whether it was there or not


// generate the final report of information
// we need to show the dictionary, and the total number of times
// each word was looked up
// we also need to compute and show the average number of lookups per word.
   // go through the hits array and sum up the hits
   // divide by maxWords to get the average
   // produce HTML showing the words in the dictionary and the number of hits on each
   // produce an HTML line telling the average

Now to write your program, all you need to do is start with this 2nd refined script and start filling in code to go with the comments.