Friday, January 9, 2009

Basic Structure of a Program : Sequence

Basically a program(algorithm) have three basic structures : sequence, selection, and repetition. We are going to discussed these basic structures by using examples. So that you will have basic and hopefully thorough knowledge of each structure.

Sequence
A Sequence consists of several instructions. Each instruction is executed according to its writing order. Instruction sequence determines the result of an algorithm. This is a sample of simple algorithm :
1.Put the pan on the stove
2.Turn on the stove
3.Put the butter into the pan
4.Pour in egg
5.Let it fried for a minute
6.Put the egg on a plate
Now you can have a plate of EATABLE fried egg. What would happen if you change the order. for example :
1.Put the pan on the stove
2.Turn on the stove
3.Pour in egg
4.Let it fried for a minute
5.Put the butter into the pan
6.Put the egg on a plate.
What do we have know ? BURNT UNEATABLE BITTER UGLY fried egg. Now lets have a simple program that can be compiled by using gcc compiler.

First Example : HELLO World
The example will produce simple executable binary file. Now we have a file named hello.c :

#include "stdio.h"

int main()
{
printf("Hello World");
return 0;
}

Then we need to do a compilation with statement like this :
#gcc -o hello.out hello.c

The output of this statement is hello.out, We can execute this executable by issuing this command :
#./hello.out

Afterwards the output "Hello World" will be displayed

More example
Lets make another simple program that will takes user input for calculating the area of a triangle. The source code is listed bellow :

#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatHeight = 0, floatWidth = 0;
system("clear"); /* This statement calls system's command : “clear” */
printf("\nWidth : --> ");
scanf("%f", &floatHeight);
printf("\nHeight : --> ");
scanf("%f", &floatWidth);
printf("\nArea : --> %f", floatHeight*floatWidth/2);
return 0;
}

Another example will be nice to enhance our "felling" for writing a program in sequential construct. The program is about calculating the area of a circle :
#include "stdlib.h"
#include "stdio.h"
#define PI 3.1416
int main()
{
float rad;
float area;
system("clear");
printf("Enter radius :-> ");
scanf("%f", &rad);
area = PI * rad * rad;
printf("Area :---------> %f", area);
printf("\nBye...");
return 0;
}

Another example is calculating the area of a rectangle :
#include "stdio.h"
#include "stdlib.h"
int main()
{
float width,length;
float area;
system("clear");
printf("Length :-> ");
scanf("%f", &length);
printf("Width :-> ");
scanf("%f", &width);
printf("Area :-> %f\n", length*width);
return 0;
}

We are going to have another simple example of sequence. See the source code bellow for the calculation of a volume of a cylinder :
#include "stdio.h"
#include "stdlib.h"
#include "math.h" /*contains prototype of pow (power) */
int main(void)
{
float floatHeight = 0, floatRadius = 0;
system("clear");
printf("\nHeight : --> ");
scanf("%f", &floatHeight);
printf("\nRadius : --> ");
scanf("%f", &floatRadius);
printf("\nVolume : --> %f", floatHeight*pow(floatRadius,2)*22/7);
return 0;
}
So, business as usual, compile and run it !!!