Program Design

Designing a program is very simple. All you have to do is keep in mind that you have to explain every step to the computer. So just think about how you would do it, then slow the process down. Fo example, given a list of numbers, I want to find the smallest number in the list.

So this is what I'd do.... compare all the numbers are write out the smallest one. Sounds simple, but we need to tell the computer how to do that. We also need to tell the computer where the program begins and ends as explained in class. So right now we can start our program and see what we have.

program small(input, output);                  { our program will use input from
                                                 the keyboard and output to the
                                                 monitor }

var smallest : integer;                        { This is where the smallest number
                                                 will be stored. }

begin                                          { This is our name program }
  compare_all_numbers;
  writeln('The smallest number is ',smallest);
end.
Since we know how to write something on the screen we can just use writeln to do it but we still need to compare all the numbers. Another thing we need are instructions for a user to enter the list of numbers. This can be done with writeln statements as well.
begin
  writeln('Welcome to Small Numbers R Us');
  writeln('You will first be asked to enter the number of numbers you wish to compare.');
  writeln('Then you will be asked to enter the list.');

  compare_all_numbers;
  writeln('The smallest number is ',smallest);
end.
Ok, so now we have the instructions, lets think about how we would do this. A few cases to consider:
  • 0 numbers in the list.
  • 1 number in the list.
  • more then 1 number in the list.
  • less then 0 numbers in the list.
    Well if there are 0 numbers, or less then 0 numbers we have an error and that should be reported. So first off we now have to ask the person to enter the size of the list and then check if the size is valid. Since the person now has to enter the size of the list that means we will need a new variable, lets call it size.
      repeat
        write('What is the size of the list : ');
        readln(size);
      until size > 0;
    
    Pretty simple, we repeat on asking them to enter the size of the list until we get a valid answer. So now lets say we have a valid answer we now have to read in the list and find the smallest number. How do we do that? We need some kind of loop, a loop that will count. That is a for loop. So...
      for i := 1 to size do
        compare_all_numbers;
      end;
    
    Looks like we needed to add another variable again, i to count for us. So lets compare all the numbers. How do we do this? Think if I gave you 100 pieces of paper, each with a number on and told you to find the smallest number. The way you would probably go about it is as follow.
  • 1) Take the first page and set it to the side.
  • 2) Take the next page, if the number is lower then the page aside, put that page aside.
  • 3) Otherwise, throw that page away.
  • 4) Goto 2 until you are done.
    When you are done the page you have left aside will be the lowest number. Notice that in step 1 we take the first page and put it aside automaticlly.

    So lets try to convert this into Pascal.

      read(num);
      if i = 1 then smallest := num;
      if num < smallest then smallest := num;
    
    This should do what we want if we put it in our for loop. First we look at the page (read) and if it is the first page we put it in the smallest pile. Then we check if the page is smaller then smallest we will put the new page in the smallest pile. The for loop will then start it all over. Notice again we had to add another vaiable num. So lets look at our complete program.
    program small(input, output);                  { our program will use input from
                                                     the keyboard and output to the
                                                     monitor }
    
    var smallest, size, i, num : integer;          { This is where the smallest number
                                                     will be stored. }
    
    begin                                          { This is our name program }
      writeln('Welcome to Small Numbers R Us');
      writeln('You will first be asked to enter the number of numbers you wish to compare.');
      writeln('Then you will be asked to enter the list.');
    
      repeat                                       { This section will make sure you enter }
        write('What is the size of the list : ');  { a positive number for the size of the }
        readln(size);                              { list. }
      until size > 0;
    
      writeln('Please enter the list of numbers.');
    
      for i := 1 to size do begin                  { This section will go through the list }
        read(num);                                 { and continually read and compare to }
        if i = 1 then smallest := num;             { find the smallest number. }
        if num < smallest then smallest := num;
      end;
    
      writeln('The smallest number is ',smallest); { Displays our answer. }
    end.
    
    And the program runs perfectly.