Saturday, November 24, 2007

Chapter 1, Lesson 3

Welcome to Beginners Programming.

A bit of c language, I'm just going to give you an idea what a C program looks like, but our C language will be on the later chapters, for we are going to dig first the flowchart. Well you can also skip to lesson 4 if you like.

Our lesson now is to display a STRING somewhere on the screen. Well, we can use escape characters but I’m going to use the gotoxy function on our tutorial.

Whenever I say “hello world!”, I am also referring to alphanumeric (0-9, a-z, A-Z etc..) characters enclosed with quotes “ ”, which is also called as STRING.

GotoXY is a function or procedure that positions the insertion or cursor at (X,Y). X in horizontal, Y in vertical direction relative to the origin of the current window. The origin is located at (1,1), the upper-left corner of the window.


DOS based program has a number of characters across and down. 80 characters across (horizontal) and 25 characters in down (vertical).


Figure 1


We just imagine were graphing lines during our geometry lessons. But in geometry, the (1,1) coordinates starts at the bottom left. In our case we use the top left corner as our (1,1) coordinates. Remember that x-axis is 80 characters and the y-axis is 25 characters. Now if we display a STRING at location (40,12), it simply means to position the cursor 40 characters in the X position and 12 characters in the Y position. Let’s say we print the STRING A (“A”) in the center of the screen.


Flowchart:

Figure 2



Code:



#include <stdio.h>
#include <conio.h>

int main(void)
{
gotoxy(40,12);

printf(“A”);
return 0;

}


Output :

Figure 3

1----------------------------------------40-------------------------------------------80



As you will notice, I included the conio.h file, where the gotoxy was defined.

Lets try printing a simple program displaying Christmas Tree using “*” .

Flowchart:

Figure 4

Code:




#include <stdio.h>
#include
<conio.h>
int main(void)
{

clrscr();
printf(“ * ”);
printf(“ *** ”);
printf(“ ***** ”);
printf(“ ******* ”);
printf(“ ********* ”);
printf(“***********”);
printf(“ * ”);
printf(“ * ”);
return 0;
}


Output :

Figure 5


How can we center the tree? By using gotoxy function we can center the tree on the output screen.


clrscr(); - this function is used to clear the screen.




Code:


#include <stdio.h>
#include
<conio.h>

int main(void)
{
clrscr();
gotoxy(35,3); printf(“ * ”);
gotoxy(35,4); printf(“ *** ”);
gotoxy(35,5); printf(“ ***** ”);
gotoxy(35,6); printf(“ ******* ”);
gotoxy(35,7); printf(“ ********* ”);
gotoxy(35,8); printf(“***********”);
gotoxy(35,9); printf(“ * ”);
gotoxy(35,10); printf(“ * ”);
return 0;
}


Output:
Figure 6

There you go… The tree is now centered…

There are lots of things you can do with the gotoxy like displaying a box, a triangle or something else you want. Go ahead and try some different shapes.

Next lesson, more on flowcharting...

-End of lesson 3 Beginners Programming

No comments: