Linux
Apache
MySQL
PHP

CSS
XHTML1.1
XML/RSS

Creative Commons

2007-10-08 20:42:30

C Programming 101

So you're in your intro to programming course, writing in C, and you're given an assignment that will actually make you think. You need to write a program that will produce output like this over and over again:
* ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** **************** ***************** ****************** ******************* ******************** ******************* ****************** ***************** **************** *************** ************** ************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** **************** ***************** ****************** ******************* ******************** ******************* ****************** ***************** **************** *************** ************** ************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** *
It looks simple. But it's a pretty good exercise in your ability to remember your way through multiple if's, while's, and for's. For those new to C, or programming in general, here's the code I worked up for this (during my master's class tonight) that gets the job done:
#include <stdio.h> int main() { int tooBig = 0; int count,x,y; while(1==1) { if(tooBig == 0) { count = 1; while(count < 20) { for(x = 1; x <= count; x++) { printf("*"); } printf("\n"); usleep(10000); count++; } tooBig = 1; } else { count = 20; while(count > 1) { for(y = 1; y <= count; y++) { printf("*"); } printf("\n"); usleep(10000); count--; } tooBig = 0; } } return 0; }
Pretty simple, but it's easy to get lost in the logic. While thinking of how to accomplish this, I realized I was going to want some way of knowing whether my line of asterisk's needs to get bigger or smaller, so I created the tooBig variable. It's value is 0 if my line needs to grow and 1 if my line needs to shrink.
The point of this program was to make the printout go forever, so that's why all of the code is between a while(1==1) block, because 1 will equal 1 forever. Then, using my previous tooBig variable, I knew I needed to write two sections for this to work. One section will print the line of asterisk's as it grows and one will print it as it shrinks. At the end of each block here I alternate the value of tooBig so that the next time through the loop it will do the opposite of what it just did (grow or shrink).
The inside blocks are very similar. I set a counter variable so I know how many asterisks to print on each line and have loops to do the printing.
You may be wondering why I call the usleep function. Well, first off you don't pronounce it YOU-SLEEP, it's pronounced MICRO-SLEEP. The "u" is supposed to be the micron symbol. Anyway, the normal sleep function only takes values in the form of seconds, and you can't make it sleep for less that 1 second. I wanted to slow down the output so I could see my program in action instead of going by in a blur, but 1 second between lines was horribly slow. So I used usleep to only delay 0.01 seconds between lines. That's slow enough to see the program work, but fast enough that you won't be waiting for the ketchup to come out of the bottle.
Anyway, I hope that was a helpful little lesson to beginning C programmers in syntax, logic, and thinking.

Back

1 comments


2007-10-11 22:53:08


Anonymous says...
/* bonus points for finding the bug :-) */
#include <stdio.h>
int
main ()
{
unsigned char i, j, k = 32;
char b[8];
for (i = j = 0;; i++)
{
while (1)
{
sprintf (b, "%%0%dd
", (j ? -1 : 1) * ((j ? -k : 0) + ++i % k));
if (!(i % k))
break;
printf (b, 0);
usleep (10000);
}
j = !j;
}
return -1;
}

Post a comment!

Name:
Comment: