Tail Whips

Usually, in all of programming courses I taught, I would at some point in the course take about 15 minutes of the beginning of the class to explain something to them. For those who had me for a half dozen or more courses, this talk might have gotten a bit old. But, even then, it was worthwhile for them to remember the standpoint I was teaching from.


As far as I know, all of the students I had in college came from public school. Some had attended other colleges and/or universities. So far in these tutorials, I have covered about the first third of the content/language of a typical first level C programming course. The big difference between these tutorials and my typical college class is that, they had programming assignments to do, which they handed in and I marked. To some degree, the format of all my courses forced the students to do at least some programming.

Based on this background, it is really important that you see the difference between these tutorials and some of the other subjects that you might study for ‘school’. For example, if you study history, you can do quite well on a test solely because you have a good memory. Programming does not work like that.

Programming is something you have to do to learn. The example I frequently used for this was riding a bike. You don’t learn to ride a bike by having someone else explain what all the parts of the bike are, how to ride it, or by watching someone else ride one. Because you have to train the part of your brain that controls your muscles, you have to get on the bike and ride it. Tail whips and back flips take lots and lots of practice.

In the case of programming, you have to train your mind to think in a way that is at least a little different from anything you have likely done before. In a way, it is very much like playing a game. What I found most difficult for students to grasp was the reality that they were the only intelligence in the equation. When you use your computer, you are looking at a lot of things that look pretty complicated and give the computer the appearance of being smart/intuitive. But every bit of that has been design and developed by other people. When it comes down to it, every computer is something that can do a lot of very simply operations at very high speed. It is the programmer’s job to organize those simple operations to get the computer to do something they want.

So, the next few tutorials will be programs using the things I have already talked about. In each, I’ll explain the logic I used to arrive at the program that is implemented in the tutorial. It will be up to you to try similar things to exercise your programming muscles. Here is the first one:

/* Draw a scaled X on the console or terminal window.
Takes input value of length of the arm of the X.
C Tutorials for the home ed student
Ron R.
August 2005

Consider an X which is 9 characters high and 9 characters wide

row column
0 123456789
1 \ /
2 \ /
3 \ /
4 \ /
5 X
6 / \
7 / \
8 / \
9 / \

Note that:
the \ character appears where row = column
the / character appears where (row + column) = (2 * (arm + 1))
the X appears where row = column and (row + column) = (2 * (arm + 1))

*/

#include

int main()
{

int arm; // length of arm
int width; // overall width (and height) of X
int row; // current row in printing
int col; // current column in printing

/* this is a good example of the use of a do/while
we want to collect the value from the user before
we test to see if it is within an acceptable range
so that the drawn x will fit in the screen
*/
do
{

printf(“Enter the length of the arm of the X (1-12, 0=Exit): “);
scanf(“%i”,&arm);

} while(arm < 0 || arm > 12);
// only draw if they have not entered a 0
if(arm > 0)
{

width = 2 * (arm + 1);
// give it some spacing
printf(“\n”);

// loop through each row
for(row=1;row {

// this will be repeated for each row
for(col=1;col {
// check for centre
if(row == col && (row + col) == width)
{

printf("X");

}
// check for back diagonal
else if(row == col)
{

printf("\\");

}
// check for forward diagonal
else if((row + col) == width)
{

printf("/");

}
// print an empty space
else
{

printf(" "); // try replacing the space with a period

}
}

// goto a new line
printf("\n");
}

}

return 0;

}