07 Jo P Jul 07


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

07 Jo P Jul 07

  1. 1. The Joy of Programming Writing Beautiful Programs: ASCII Arts and Fractals—Part I S.G. GANESH Computer programs can be written for aesthetic purposes also. This column gives an introduction to the basics of computer art using C, and is meant for students and novice programmers. M ost of us associate C, or any other programming $ gcc tmp/hello.c ; ./a.out language for that matter, with serious coding. However, programs can also be written just for fun @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ or aesthetic purposes—like drawing interesting patterns or @ @ @ @ @ @ @ @ @ @ @ pictures. @ @ @ @ @ @ @ ASCII art refers to drawing pictures using only ASCII @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ code. (ASCII is the most widely used character-encoding scheme for computers, and is based on letters of the What the program does is very simple. The GUEST COLUMN English alphabet.) Much of ASCII art can be generated by “HELLO” character is encoded with hexadecimal computer programs. To give you an introduction to the characters in the h array. It is a two-dimensional, concept, I’ve written a small program that prints “HELLO” 5 x 5 array, with each character to represent a bit using the ‘@’ character: pattern of four bits each. The bit-mask array m has hex characters to int main() { represent the bits (fourth, third, second and first // encode the “Hello” string as hex numbers char h[5][5] = { { 0x9, 0xE, 0x8, 0x8, 0xF }, bits) set so that they can be used to check with { 0x9, 0x8, 0x8, 0x8, 0x9 }, the bit-pattern in the character. { 0xF, 0xE, 0x8, 0x8, 0x9 }, We have to print five rows, bit-patterns for { 0x9, 0x8, 0x8, 0x8, 0x9 }, five characters (one each for the letters in { 0x9, 0xE, 0xE, 0xE, 0xF } }; “HELLO”) and four bits read the bit-pattern using the mask. The three nested loops do exactly that. // mask for reading the characters This is a simple, straightforward program. If char m[4] = { 0x08, 0x04, 0x02, 0x01 }; you go through it, I’m sure you can easily decipher the logic. The basic idea in this program int i = 0, j = 0, k = 0; is to use a multi-dimensional array to represent the bit-pattern and a mask array to decode it. In // for five rows with five characters each, // decode the hex numbers using the mask and print @ this way, you can ‘programmatically’ represent do { // for five rows complicated ASCII art, and print it using simple j = 0; logic. do { // for five chars You may have already used the banner command that k = 0; prints posters in large letters. Maybe you would want to do { // for four points find out how it works? (h[i][j] & m[k]) ? printf(“ @”): In next month’s column, we’ll take a different approach printf(“ “); and do something little more complex. We’ll draw } while(++k < 4); ‘Sierpinksi triangle fractal’ using just a few lines of code } while(++j < 5); and simple logic using recursion/iteration. printf(“n”); } while(++i < 5); } S.G. Ganesh is an engineer in Hewlett-Packard’s C++ compiler team. He is also a member of the ANSI/ISO C++ When you execute the program, you’ll see the following Standardization committee (JTC1/SC22/WG21).You can “HELLO” banner on your screen: reach him at sgganesh@gmail.com www.linuxforu.com | LINUX FOR YOU | JULY 2007 87 CMYK