QUOTE
If i can't get it there is no point to continue to study c++
Not true. Understanding other people's code is not easy. It can be hard to wrap your head about another person's logic. I think anyone who has spent any time answering post here can attest to it.
lets begin by looking at what the program does. It prints out a text-"ruler":
|...............................................................|
|...............................|...............................|
|...............|...............|...............|...............|
|.......|.......|.......|.......|.......|.......|.......|.......|
|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||It does so by inserting bars '|' into a string in the right places. Then it calls subdivide() with the level set to 1, and low and high set to the outer two bars. This causes subdivide() to insert a bar in the middle (high + low)/2 = the average = the middle. Then subdivide tries to call itself with level = 0 and low and high set to match the lower region and the upper region on the string... but since level = 0 these calls just return without doing anything. The next time it is called it has level = 2, so this time when it recursively calls itself it will add in a bar to the midpoint of the upper and lower regions. It does this by first setting low = min, high = mid (this works for the lower half), and then low = mid, high = max (this works for the upper half).
This process is called bisection and it is an important concepts in computer science. Take for instance a binary search. Lets say I want to guess what number you are thinking of between 1 and 100. When I give a guess you tell me if I am high, low, or right on.
I start by guessing in the middle of my range: 50
if you say high, then I take the middle of the lower half: 25
if you say low, then I guess the middle of the upper half: 75.
The process continues this way... me always guessing the mid point (low number + high number)/2
this quickly brackets the number.
here is a comment version of the code:
CODE
#include <iostream>
using namespace std;
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level);
int main()
{
char ruler[Len];
int i;
//--- Initialize the string to a blank --
for (i = 1; i < Len - 2; i++)
ruler[i] = ' ';
ruler[Len - 1] = '\0'; //the end of strings in c are all marked with a zero character
//--- Set up our meta-data
int max = Len - 2;
int min = 0;
//Add the two ending bars for the inital ruler...
ruler[min] = ruler[max] = '|';
//** at this point the ruler is initalized. One bar on either end.
// this never changes, only the inner characters are changed...
std::cout << ruler << std::endl;
for (i = 1; i <= Divs; i++)
{
subdivide(ruler,min,max, i);
std::cout << ruler << std::endl;
for (int j = 1; j < Len - 2; j++)
ruler[j] = ' '; // reset to blank ruler
}
cin.get();
cin.get();
return 0;
}
//This function will add a bar to the middle of the range min to max
// It will then pass the lower half to another copy of itself
// and the upper half to another copy of itself...
// The argument 'level' determines how many times this happens.
// if level == 0 it does nothing.
// if level == 1 it adds 1 bar
// if level == 2 it adds 3 bars... on in the middle, one in the upper side, on in the lower side...
void subdivide(char ar[], int low, int high, int level)
{
if (level == 0)
return;
int mid = (high + low) / 2;
ar[mid] = '|';
subdivide(ar, low, mid, level - 1);
subdivide(ar, mid, high, level - 1);
}