Securing Knowledge.

Continuosity on learning is the most important thing to secure your knowledge.

Wednesday, September 15, 2004

Function in C

Functions allow the programmer to modularize a program. The format of function definition is :

Return-value-type function-name (parameter-list)
{
declarations…..

statements…..
}


/*Function*/
/*Example -- A programmer-defined square function*/
/*Fig 5.13 -- page 147*/

#include stdio.h /* I’m not typing <> symbol cause its illegal in this blog*/

int square (int); /*Funtion prototype*/

int main ()
{
int x;

for ( x = 1; x <= 10; x++)
printf ("%d ", square (x)); /*calling a function*/
printf ("\n");
return 0;
}

int square (int y) /*Function*/
{
return y*y;
}

carrotsusu, 11:59 PM | link | 3 comments |

Examples -- repetition structure

Example – “do-while” structure

#include stdio.h /*I’m not typing <> symbol cause its illegal in this blog*/

int main ( )
{
int counter = 1;

do {

printf ("\n\n%d",counter);

} while ( counter++ <= 10);

return 0;

}

Example – “for” structure

#include stdio.h /*I’m not typing <> symbol cause its illegal in this blog*/

int main ()
{
int x, y, jumlah = 0;

for ( x = 1; x <= 6; x++)
{
scanf ("%d\n", &y);
jumlah = jumlah + y;
}

printf ("\nJumlah nombor yang dimasukkan ialah: : %d\n", jumlah); }

Example – “for” structure with “break” statement

#include stdio.h /*I’m not typing <> symbol cause its illegal in this blog*/

int main ()
{
int x;

for ( x = 1; x <=10; x++ )
{
if ( x == 5)
break; /*Break out if x adalah sama dengan 5*/
printf ("%d", x);
}
printf ("\n Broke out of loop at x == %d\n", x);
return 0;
}

Example – “for” structure with “continue” statement

#include stdio.h /*I’m not typing <> symbol cause its illegal in this blog*/

int main ()
{
int x;

for ( x = 1; x <= 10; x++)
{
if ( x == 5 )
continue;
printf (" %d ", x);
}
printf ("\nUsed continue to skip the printing value 5\n");
return 0;
}

carrotsusu, 11:49 PM | link | 9 comments |

C program control - for, switch, while, do-while.

Consist of “for” repetition structure, “switch” multiple selection structure, and “do/while” repetition structure.

Essentials of repetitions

Most program involve repetition or looping. A loop is a group of instructions the computer executes repeatedly while some loop continuation condition remains true. Two means of repetition are 1. counter-controlled repetition and 2. Sentinel-controlled repetition.

Counter controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed. And otherwise in sentinel-controlled repetition – we don’t know in advance how many time the loop will be executed. Counter-controlled repetition – control variable is used to count the number of repetitions. Sentinel values used to control repetition when 1. the precide number of repetitions is not known in advance, and 2. the loop includes statements that obtain data each time the loop is performed.

Example of switch multiple-selection structure

#include stdio.h /* I’m not typing <> symbol cause its illegal in this blog*/

int main ()
{
int grade;
int aCount = 0, bCount = 0, cCount = 0, dCount = 0, eCount = 0, fCount = 0;

printf ("Enter the letter grades.\n");
printf ("Enter EOF character to end input.\n");

while (( grade = getchar( )) != EOF)
{
switch (grade)
{
case 'A': case 'a':
++aCount;
break;

case 'B': case 'b':
++bCount;
break;

case 'C': case 'c':
++cCount;
break;

case 'D': case 'd':
++dCount;
break;

case 'E': case 'e':
++eCount;
break;

case 'F': case 'f':
++fCount;
break;

case '\n': case ' ':
break;

default:
printf ("Incorrect letter grade entered.");
printf ("Enter a new grade.\n");
}
}

printf ("\nTotal for each letter grade are:\n");
printf ("A: %d\n", aCount);
printf ("B: %d\n", bCount);
printf ("C: %d\n", cCount);
printf ("D: %d\n", dCount);
printf ("E: %d\n", eCount);
printf ("F: %d\n", fCount);

return 0;
}

EOF character in Window$ is Ctrl-Z or Ctrl-C. In Unix/Linux environment is Ctrl-C (correct me if I’m wrong…)

carrotsusu, 10:30 PM | link | 0 comments |