Structure is a bunch of variables bundled together and then is named according to the proper abstraction of the creator. Therefore the name of a structure depends on the creator. Of course the name should be meaningful. The variables may be of the different type. In another languange a structure may be called a record. In my opinion, the most important aspect of structure is for abstraction. Compiling attributes that are important for current activities, and forget about the unimportant things. For example I need to model a student, and what matters to me for deciding his final mark are : StudentId, First Name, Last Name, Assignment, MidExam, and FinalExam. In this example the weight for final mark calculation is 30% of assignment, 30% of MidExam, and 40% of FinalExam. The name of the structure is Student. See the example bellow :
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
char StudentId[5];
char FirstName[20];
char LastName[20];
float Assignment;
float MidExam;
float FinalExam;
} Student;
int main(void)
{
Student os;
float final;
printf("Student Id : ");
scanf("%s", os.StudentId);
printf("First Name : ");
scanf("%s", os.FirstName);
printf("Last Name : ");
scanf("%s", os.LastName);
printf("Assignment : ");
scanf("%f", &os.Assignment);
printf("Mid Term Exam : ");
scanf("%f", &os.MidExam);
printf("Final Exam : ");
scanf("%f", &os.FinalExam);
final = 0.3*os.Assignment+ 0.3*os.MidExam+ 0.4*os.FinalExam;
printf("The Grade for : %s %s %s is %f", os.StudentId, os.FirstName, os.LastName, final);
}
Showing posts with label C Programming : Basic Structure. Show all posts
Showing posts with label C Programming : Basic Structure. Show all posts
Saturday, January 17, 2009
Tuesday, January 13, 2009
Basic Structure of a Program : Iteration
Iteration
The last important construct is itteration. It executes a set of programming statements several times until certain condition is reached. The sentinel condition is usually evaluated by using pre increment and post increment Operation. Lets have a look at the example bellow. It shows the post increment operation in a simple sequential program :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int i = 10;
system("clear");
printf("%d", i++); /*The output of this operation is 10, since the variable is used first and then the value is incremented */
}
Now we are going to have a pre increment operation :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int i = 10;
system("clear");
printf("%d", ++i); /*The output of this operation is 11, since the variable is incremented first and then the value is displayed */
}
Now lets use this operation for controlling a for loop. Example below is post increment :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int j;
system("clear");
for (j=0;j<10;)
{
printf("%d \n", j++);
}
}
The output of the program is : 0 1 2 3 4 5 6 7 8 9. What would happened if the sentinel is pre increment
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int j;
system("clear");
for (j=0;j<10;)
{
printf("%d \n", ++j);
}
}
The output will decidedly different : 1 2 3 4 5 6 7 8 9 10.
Now its time for use to move to the nitty gritty of C programming.
The last important construct is itteration. It executes a set of programming statements several times until certain condition is reached. The sentinel condition is usually evaluated by using pre increment and post increment Operation. Lets have a look at the example bellow. It shows the post increment operation in a simple sequential program :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int i = 10;
system("clear");
printf("%d", i++); /*The output of this operation is 10, since the variable is used first and then the value is incremented */
}
Now we are going to have a pre increment operation :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int i = 10;
system("clear");
printf("%d", ++i); /*The output of this operation is 11, since the variable is incremented first and then the value is displayed */
}
Now lets use this operation for controlling a for loop. Example below is post increment :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int j;
system("clear");
for (j=0;j<10;)
{
printf("%d \n", j++);
}
}
The output of the program is : 0 1 2 3 4 5 6 7 8 9. What would happened if the sentinel is pre increment
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int j;
system("clear");
for (j=0;j<10;)
{
printf("%d \n", ++j);
}
}
The output will decidedly different : 1 2 3 4 5 6 7 8 9 10.
Now its time for use to move to the nitty gritty of C programming.
Labels:
C Programming : Basic Structure
Friday, January 9, 2009
Basic Structure of a Program : Selection
Basic Structure of a Program : Selection
Selection construct in any programming language is for controlling the execution of certain part of an algorithm when certain condition occurs. In C/C++ language this structure is implemented with if/if-else if-else and switch-case statements. For example we decide not to drink tea when it is hot, we eats when we feel hungry, A lecturer decides that a student passes an exam if his mark is more or equal to 60. Bellow is an example of if construct :
First Example : if
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00)
{
printf("Passes\n");
}
else
{
printf("Fails\n");
}
return 0;
}
Second Example : if/else-if/else
In the next example we specify that a student get :
1.Grade C if his mark is more or equal 60 and less than 70
2.Grade B if his mark is more or equal 70 and less than 80
3.Grade A if his mark is more or equal 80 and less or equal than 100
4.Fails if his mark is more or equal 0 and less than 60
5.The program will say that the value is not legal if it is not in the range between 0 and 100.
The sorce code is :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00 && floatGrade < 70) /*Condition for grade C*/
{
printf("Passes with grade C\n");
}
else if (floatGrade >= 70.00 && floatGrade < 80) /*Condition for grade B*/
{
printf("Passes with grade B\n");
}
else if (floatGrade >= 80.00 && floatGrade <= 100) /*Condition for grade A*/
{
printf("Passes with grade A\n");
}
else if (floatGrade >= 0.00 && floatGrade < 60) /*Condition for Fail*/
{
printf("Fails \n");
}
else /*Specifying that another value outside the range of 0 to 100 is not legal*/
{
printf("Value is not legal\n");
}
return 0;
}
Third Example : switch-case
Another selection type is using switch-case. A switch-case construct consists of :
1.switch key word accompanied by the variable that will be evaluated to determine which program statement that will be executed.
2.program statements inside the switch-case nest.
The example of this statement is that a student will be awarded a sum of money, so that he can go to the movie.
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h" /*We use toupper to convert the lowercase input to uppercase*/
int main()
{
char chrGrade;
system("clear");
printf("Student Grade :---> ");
scanf("%c", &chrGrade);
chrGrade=toupper(chrGrade);
switch(chrGrade)
{
case 'A' : printf("You are awarded $100");break;
case 'B' : printf("You are awarded $50");break;
case 'C' : printf("You are awarded $25");break;
case 'D' : printf("You are awarded $0");break;
default : printf("Wrong Grade");break;
}
return 0;
}
Now you have already learn how to create a selection construct.
Selection construct in any programming language is for controlling the execution of certain part of an algorithm when certain condition occurs. In C/C++ language this structure is implemented with if/if-else if-else and switch-case statements. For example we decide not to drink tea when it is hot, we eats when we feel hungry, A lecturer decides that a student passes an exam if his mark is more or equal to 60. Bellow is an example of if construct :
First Example : if
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00)
{
printf("Passes\n");
}
else
{
printf("Fails\n");
}
return 0;
}
Second Example : if/else-if/else
In the next example we specify that a student get :
1.Grade C if his mark is more or equal 60 and less than 70
2.Grade B if his mark is more or equal 70 and less than 80
3.Grade A if his mark is more or equal 80 and less or equal than 100
4.Fails if his mark is more or equal 0 and less than 60
5.The program will say that the value is not legal if it is not in the range between 0 and 100.
The sorce code is :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00 && floatGrade < 70) /*Condition for grade C*/
{
printf("Passes with grade C\n");
}
else if (floatGrade >= 70.00 && floatGrade < 80) /*Condition for grade B*/
{
printf("Passes with grade B\n");
}
else if (floatGrade >= 80.00 && floatGrade <= 100) /*Condition for grade A*/
{
printf("Passes with grade A\n");
}
else if (floatGrade >= 0.00 && floatGrade < 60) /*Condition for Fail*/
{
printf("Fails \n");
}
else /*Specifying that another value outside the range of 0 to 100 is not legal*/
{
printf("Value is not legal\n");
}
return 0;
}
Third Example : switch-case
Another selection type is using switch-case. A switch-case construct consists of :
1.switch key word accompanied by the variable that will be evaluated to determine which program statement that will be executed.
2.program statements inside the switch-case nest.
The example of this statement is that a student will be awarded a sum of money, so that he can go to the movie.
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h" /*We use toupper to convert the lowercase input to uppercase*/
int main()
{
char chrGrade;
system("clear");
printf("Student Grade :---> ");
scanf("%c", &chrGrade);
chrGrade=toupper(chrGrade);
switch(chrGrade)
{
case 'A' : printf("You are awarded $100");break;
case 'B' : printf("You are awarded $50");break;
case 'C' : printf("You are awarded $25");break;
case 'D' : printf("You are awarded $0");break;
default : printf("Wrong Grade");break;
}
return 0;
}
Now you have already learn how to create a selection construct.
Labels:
C Programming : Basic Structure
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 !!!
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 !!!
Labels:
C Programming : Basic Structure
Subscribe to:
Posts (Atom)
