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 |

The if/else selection structure

I think there’s a better way to write the code below. Sorry, I forgot where’s the question is…

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


int main ()
{
int x, y, z, sum, avg, product;

printf ("Masukkan 3 integer : ");
scanf ("%d%d%d", &x, &amp;y, &z);

sum = x+y+z;
printf ("Sum is : %d\n", sum);

avg = sum/3;
printf ("Avg is : %d\n", avg);

product = x*y*z;
printf ("Product is : %d\n", product);

if (x > y)
if (x > z)
printf ("Largest : %d\n", x);
if (y > x)
if (y > z)
printf ("Largest : %d\n", y);
if (z > x)
if (z > y)
printf ("Largest : %d\n", z);
if (x <>
if (x <>
printf ("Smallest : %d\n", x);
if (y <>
if (y <>
printf ("Smallest: %d\n", y);
if (z <>
if (z <>
printf ("Smallest : %d\n", z);

return 0;
}
carrotsusu, 9:27 PM | link | 0 comments |

Exercise – repetitions (looping).

Question : Question -- Write a program that calculates and prints the average of several integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequence might be :

(10 8 11 7 9 9999)

I succeed in answering it. Congrates carrotsusu!!! hehehe :p

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

int main ()
{

int x, y, jumlah, purata;

for ( x = 0; x <= 6; x++ )

{
scanf ("%d", &y);
if ( y == 9999) break;
jumlah = jumlah + y;
/*printf ("%d", x);*/
}

purata = jumlah / x;
printf ("\npurata keseluruhan nombor yang dimasukkan ialah: %d\n", purata);
return 0;
}
carrotsusu, 9:02 PM | link | 0 comments |

Logical operators.

We will appreciate our knowledege when we try to gain it back alone, instead of learn-back from the guru’s. This sentence came into my mind when i strugling to understand the word in a book – in learn-back C programming. I feel like i’m loosing something when i realized that i’m not in full concentration when lecturers giving a lecture about C programming 3 years ago... hemph... what a waste of time!

So, kids out there... try to concentrate to your teacher when they try to teach you something... don’t be like me... hehehe :p
Enuff of motivational stuff... continue learning C... heh :p... today is about logical operators

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

int main ( )
{
int num1, num2;

printf ("Masukkan 2 nombor, dan saya akan beritahu\n");
printf ("hubungan antara keduanya:");
scanf ("%d%d", &num1, &amp;num2);

if (num1 == num2)
printf ("%d adalah sama nilai dengan %d\n\n\n", num1, num2);

if (num1 != num2)
printf ("%d adalah tidak sama nilai dengan %d\n\n\n\n", num1, num2);

if (num1 <> num2)
printf ("%d adalah lebih besar dari %d\n\n\n", num1, num2);

return 0; /*indicate program ended successfully*/
}

continue… :p

carrotsusu, 8:20 PM | link | 0 comments |

Monday, September 13, 2004

Code for print and scan for user input

Well, enough for helloworld stuff, go for the next learn-back code... hehe :p. It's for how to print and scan for user input.

#include stdio.h /* i cannot put the <> symbol arounf stdio.h (it's illegal in this blog, hehe... :p)*/
int main ()
{
int num1, num2, jumlah;
printf ("Masukkan nombor pertama\n");
scanf ("%d", &num1);
printf ("Masukkan nombor kedua\n");
scanf ("%d", &num2);
jumlah = num1 + num2;
printf ("Jumlah dua nombor tadi ialah %d \n", jumlah);
return 0;
}

continue... :)

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

C compiler & setup -- My first step

I use DJGPP as the compiler for my C's code. DJGPP compiler can be found here. The tutorial about DJGPP can be found here.

I use DJGPP compiler because i'm quite comfortable with it. There's a lot of C compiler. You all can search it at Google... hehehe :p

So, continue to my first learn-back code. Its anybody-know code : the hello world code...
#include stdio.h /* i cannot put the <> symbol arounf stdio.h (it's illegal in this blog, hehe... :p)*/
int main (void)
{
printf ("hai, mari start balik coding C\n");
return 0;
}

carrotsusu, 9:00 PM | link | 0 comments |

Author's intro & Blog purpose

Assalamualaikum and good day,
I'm sorry if my English is bad. I use this blog as a tool to improve my English... :p
A little bit of me, my name is Zuraidy Adnan (carrotsusu, laicibuah, onjoy). I live at Tanjong Malim, Perak, Malaysia. And I'm totally Malaysian, and I'm proud to be Malaysian. Age 25, male, single and available. I like programming. My ambition is to be a good programmer. The reason i'm creating this blog is to keep track to what i've learn about programming. The first programming language that i've learn while i'm in university is C programming. 2 weeks ago, after 4 years left the university, i feel that if i want to secure my programming skills and knowledge, i must keep learning the code. I felt like i start to forgot almost all about programming. So i try to catch my knowledge back. I feel that continuosity is the most important thing to secure our knowledge.
My working experience :
Published paper :
Outdoor Activities :
carrotsusu, 8:00 PM | link | 0 comments |