You need to write a program to output a ruler’s calibration. You cannot simply output the results using

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

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:

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

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

500 years after you dispose of an aluminum can, it still exists.

◀ Divide and Conquer▶ How to Write a Function Efficiently