Four Step Programming Model: An Example

◀ Divide and Conquer▶ How to Write a Function Efficiently
Amazon Let's look at an example and apply the Four Step Programming Model to see it work in action! Meanwhile we'll be applying the divide-and-conquer concept we mentioned in the previous article.

You need to write a program to output a ruler’s calibration. You cannot simply output the results using cout; otherwise there won’t be any fun and frustration. The program must output the following:
|                               |
|               |               |
|       |       |       |       |
|   |   |   |   |   |   |   |   |
| | | | | | | | | | | | | | | | | |||||||||||||||||||||||||||||||||
After you examine the output, you should have some idea how to implement the program. Apply the four steps and see if you are up for the challenge. The following is the results of my following the four steps:

Step 1
A close inspection tells us that a loop can accomplish this job by first outputting a | in the leftmost and rightmost positions, then calculating the distance from leftmost point to the middle point for next iteration, which is half the distance between the two |’s.


Once we have the distance, for every amount of that distance starting at the leftmost point, we output a |. In the next iteration, we find the distance between one | and the next |, cut it in half, and output a | for every amount of that distance starting at the leftmost point.

This process goes on until the distance becomes 1, which we have reached the bottom. Having this in mind, Let’s go ahead and apply the first step. It requires us to draft a skeleton of the program, which should look something like this:
int main(){
-declare a char array and fill it with space, don’t forget to terminate it with ‘\0’ -obtain distance between leftmost and rightmost positions of the ruler; in this case, it should be 0 + 32, or 32. -use a loop; while the distance is greater than zero, the following happens: fill the array with | at each multiple of the distance output that array update distance by dividing it by two }
Step 2
Let’s decide what variables and objects we need to store data. As the skeleton suggests, we need a char array, an index for the for loop, and an int for distance. So here it goes:
int main(){
char ruler[33]; int dist; -declare a char array and fill it with space, don’t forget to terminate it with ‘\0’ -obtain distance between leftmost and rightmost positions of the ruler; in this case, it should be 0 + 32, or 32. int i; -use a loop; while the distance is greater than zero, the following happens: fill the array with | at each multiple of the distance output that array update distance by dividing it by two }
Step 3

Here is a breakdown of this step and the corresponding observations:

Interrelation: Whenever ruler uses i as the index of a loop, i can never go out of ruler’s bounds. When i is used as a distance increment, make sure it gets the right value.

State: ruler should be filled with spaces initially, then contain increasingly more |’s until it contains nothing but |. i may be used for indexing more than once, so make sure whenever you start using it, it is set to the beginning of the array. dist should be 32 initially and cut in half in each round of printing the ruler.


Scope: All variables can stay local.

Step 4
Here is the complete program, including as comments the above skeleton:
#include<iostream>
using namespace std;

int main(){
  char ruler[33];
  int i,dist;

  /* 
  declare a char array and fill it in with space 
  */
  for(i=1;i<33;i++)
    ruler[i]=' ';
  /* 
  obtain distance between leftmost and rightmost positions of the ruler; in this case, it should be 0 + 32, or 32.
*/ dist=32; while(dist>0){ /* fill the array with | at each multiple of the distance */ for(i=0;i<33;i+=dist) ruler[i]='|'; /* output that array */ cout<<ruler<<endl; /* update distance by dividing it by two */ dist/=2; } return 0; }
This program uses a char array, but you don’t have to use an array; you can simply calculate the appropriate distance and use cout to output a | or a space.


These four steps work for me, and they should work for you too. Make sure you practice often so that you can begin to program finitely, the sole purpose of reading this book! Next we’ll look at how we can apply the same set of concepts to composing a function!

500 years after you dispose of an aluminum can, it still exists.
◀ Divide and Conquer▶ How to Write a Function Efficiently

fShare
Questions? Let me know!