20 November 2013

C Functions

Today, I am going to discuss about "Functions".

Functions are a group of statements that is created to perform a special task and returns a value as its result.
Example: The main() function.


From the above definition, you can get:

-Function is a group of statements.

-Function performs special tasks.
 Example: The factorial function is used to calculate the factorial of a given number.

-Function returns a value.
 Please note this: Functions return one value only no matter how many arguments it has as its input(s).


So, how do we create a function?

Use this syntax:
return_type function_name (data_type var1, data_type var2, ....){
 //statement1;
 //statement2;
 //...
return data
}

Let's go straight to creating our own function.
I would like to show you how to create a factorial function by using iteration rather than recursion.

First of all, I need to explain what factorial is and how it works.

n factorial, denoted by n!, is the factorial of n which equals to n * (n-1) * (n-2) * ... * 3 * 2 * 1.

So, 5! = 5*4*3*2*1 = 120 and 7! = 7*6*5*4*3*2*1 = 5040.

If you analyze it thoroughly, you will see that 5! can also be 1*2*3*4*5 and it still equals to 120. The same thing works for 7! too.

Therefore, we can conclude that:
n! = 1 * 2 * 3 *.... * (n-2) * (n-1) * n
We have learned about the For Loop before, now we are going to put it into use.


How are we going to calculate the factorial of n?

It's simple, all we have to do is loop from 1 to n, multiply each values respectively, store it in a variable, then return the result.


The code would be like this:
int fact(int x){
 int i=1;
 int result=1;
 for(i=1; i<=x; i++){
   result = result * i;
 }
 return result;
}

Explanation:
Integer x is the input, Integer i is the loop counter, Integer result is the value that will be returned as the result of the operation performed.

Let me simulate the flow of this program.

Let the input (x) be 5. The program will loop for 5 times.

Loop 1 :
i = 1, result = 1*1 = 1

Loop 2 :
i = 2, result = 1*2 = 2

Loop 3 :
i = 3, result = 2*3 = 6

Loop 4 :
i = 4, result = 6*4 = 24

Loop 5 :
i  = 5, result = 24 * 5 = 120 (answer)


Assuming you want to display your result, you would want to create your main function. The whole program will look like this :

#include <stdio.h>
#include <stdlib.h>


int fact(int x){
 int i = 1;
 int result = 1;
 for(i = 1; i <= x; i++){
   result = result * i; 
 }
 return result;
}


int main(){
  printf("%d\n",fact(5)); 
  system("pause"); 
  return 0; 
}

Description of the code:
The main program prints the factorial of 5. If you want it to print the factorial of different number, you can do so by changing the 5 to another number.

Please note that he result variable is an Integer and an Integer has its limit. It will not be able to calculate the factorial of large numbers. If you wish to calculate the factorial of large numbers, you might want to change the return_type of the function.

Anyway, here's a preview of the program we have just created:





Happy Coding,
Cyber Frost

16 November 2013

Binary Conversions

Hello Coders.

Today I would like to discuss about binary digits.
If you don't know what a binary digit is, here is a simple explanation:

A Binary Digit is a digit of number whose value is either 0 or 1.

In computer, the 0s and 1s indicate electrical signals.
1 indicates ON, 0 indicates OFF.

As we have learned in the previous post about data types,  a character has a size of 1 byte.
1 byte equals to 8 bits.

Basically a set of binary digits make up 1 byte, or 8 bits.

Examples: 1111 1111, 0000 1010, 1010 1110, etc.

What can we do with those 1s and 0s?

You can convert them to decimals, octals, and hexadecimals.
Also, you can use the AND, OR, and XOR operators to perform special conversions.

Converting Binary to Decimal 

Each digit of binary represents a decimal value which is 2n, where 0 <= n < 8 .

The leftmost digit in an 8-bit set of binary digits has the value of 27 while the rightmost has the value of 20. The digits in between has the value from 26 to 21 in a left-to-right order.

Let's go right to the examples:
  • 11111111 = 1*2^7 + 1*2^6 + 1*2^5 + 1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 255
  • 00000001 = 0*2^7 + 0*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 1
  • 01010101 = 0*2^7 + 1*2^6 + 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 85
  • 00110010 = 0*2^7 + 0*2^6 + 1*2^5 +1*2^4 + 0*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 50

So let's say you have sets of 3-bit binary digits:
  • 111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
  • 010 = 0*2^2 + 1*2^1 + 0*2^0 = 2
  • 000 = 0*2^2 + 0*2^1 + 0*2^0 = 0
  • 100 = 1*2^2 + 0*2^1 + 0*2^0 = 4 

 Or sets of 4-bit binary digits:
  • 0111 = 0*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 7
  • 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10
  • 0100 = 0*2^3 + 1*2^2 + 0*2^1 + 0*2^0 = 4
  • 1001 = 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 9
 There you go. You are now capable of converting binary digits to its decimal value.


Converting Binary to Octal

In converting binary(base-2) to octal(base-8), you need to understand that octals are numbers ranging from 0-7 only. This means that 777 exists while 008 doesn't.

To convert an 8-bit binary to its octal value, you need to separate it into 3-bits at a time, starting from the right. Like this:
1111 1111 =  11 | 111 | 111
Three bits at a time? Why is there a 2-bit part then? Simple add 0 in front to make it 3-bits. Notice that 011 == 11 in binary. So, here is the digits after separation:
011 | 111 | 111
Then what?

Calculate each part, convert them to its decimal value.

011 = 2+1 = 3
111 = 4+2+1 = 7
111 = 4+2 +1 = 7

So, 11111111(2) = 377(8)

Another example:
1010 1010 = 010 | 101 | 010 = 252(8)

Converting Binary to Hexadecimal

Hexadecimal numbers range from 0 to F: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
In order to convert to Hexadecimals(base-16), you will need to separate the bits four at a time.

Example:
1111 1111(2) = ..... (16)

= 1111 | 1111 
= 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 | 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0
= 15 | 15
= FF(16)

1001 1100(2) = ..... (16)

= 1001 | 1100
= 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 | 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 9 | 12
= 9C(16)

Using the AND(&&) Operator

Here is what the AND operator does when two values are passed to it.

p   q   p AND q
   1       1         1
1   0         0
0   1         0
0   0         0

Let's assume we have two sets of binary digits: 11110101 and 00110101. Using the AND operator, we will receive the result:

11110101
00110101 AND
00110101

Another example with 00001110 and 11101100.

00001110
11101100 AND
00001100


Using the OR(||) Operator

The table of OR looks like this:

p   q   p AND q
   1       1         1
1   0         1
0   1         1
0   0         0

So, if your perform an OR on 11110000 and 00001111, you will get:

11110000
00001111 OR
11111111
It is similar to the AND operator, only with different rules.

Using the XOR(^) Operator

The XOR Operator is also called the Exclusive OR. Its table has values like this:

p   q   p AND q
   1       1         0
1   0         1
0   1         1
0   0         0

It can be used to swap digits. Let me show you.


Let A be 00110011 and B be 01011100.
We would like to swap the value of A and B.
We can use the XOR Operator to accomplish the task, like this:

A = A ^ B
B = A ^ B
A = A ^ B

The formula can also be simplified as A^=B^=A^=B

The process looks like this:
A = 00110011
B = 01011100 XOR
A = 01101111

A = 01101111
B = 01011100 XOR
B = 00110011 ------------------- B is now A

A = 01101111
B = 00110011 XOR
A = 01011100 ------------------- A is now B
These concepts can be applied to your programming. For example you can create a swap function using the XOR operator.


Good luck trying, pals.



Regards,
Cyber Frost

09 November 2013

The For Loop in C

The loop is one of the most critical parts of programming.
According to Wikipedia, a loop is a sequence of statements which is specified once but which may be carried out several times in succession. Read more about Loops here.

So, basically, a loop is a statement used to repeat one or more actions for a specified number of times.

There are two types of loop :
  1. Finite Loop
  2. Infinite Loop
When looping, in most cases, you would want to avoid infinite loops because it might crash your whole system.

There are actually several kinds of Loops in C which are the For loop, the While loop, and the Do...While Loop. But today, we are going to discuss about the For loop.


Let's go straight to the syntax of declaring a for loop:
for(initial_condition;loop_condition;increment){
//statement1
//statement2
//....
}
The initial condition is set by assigning a value to a variable, called Control Variable.
The loop condition is the condition to be checked at the start of every loop cycle. If the condition is true, continue the looping process, else, break(or stop) the process.
The increment is the value added to the Control Variable at the end of every loop cycle.
The statements within the parentheses are the statements to be executed in every cycle of the loop.


Example:

Output numbers from 1 - 10 using no more than 1 printf function.
 #include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop
 for(i = 1; i <= 10; i++){
     printf("%d\n", i);  
    }
   
 system("Pause");  
 return 0;  
}
Magically, it works. Here is the result :


Let me explain the loop declaration part.

There is i = 1, this means, the count starts from 1.
Then, there is i <= 10, this indicates that the loop will only end after i is larger than 10. If i is still in range (1-10), the loop will continue to display i, which is incremented by 1 every cycle by using the i++ statement.

Note : You can also use +1 increment this way -> i += 1


Okay, let's mess a little with the increment part of loop now.
Let us create two loops to display odd and even numbers from 1-10, respectively.

#include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop for odd numbers

 printf("Odd numbers:\n");
 for(i = 1; i <= 10; i += 2){
     printf("%d\n", i);  
    }

 //declaration of for loop for even numbers  
   
 printf("\nEven numbers:\n");  
 for(i = 2; i <= 10; i += 2){
     printf("%d\n", i);  
    }  
   
   
 system("Pause");  
 return 0;  
}

As you can see from the code, there are two for loops having the same increment but producing different results. If you look closely, you will find that the only difference is in the initial value of both loops. To obtain odd numbers, you start from 1 and add by 2 each loop. However, in order to display even numbers, you need to start from 2(the first even number in range) and increment it by 2 through the loop. You can see the result below:



To close this post, I would like to present you with an example of creating an infinite loop.
Despite the fact that we are doing it in purpose right now, it might happen a lot as accidents in programming later on. Here it is:

#include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop for odd numbers

 for(i = 1; i <= 10; i--){
     printf("*");  
    }
    
   
 system("Pause");  
 return 0;  
}

 It prints the * symbol endlessly. Why? As you can see, the loop condition is i <= 10 while the loop increment is i--. This ensures that the i never reaches 10 or above(not even 2 actually).

Note: To stop the program, press Ctrl + C


Loops can be used for many occasions, you have to experiment by yourself in order to be fluent in using loops. Any questions are welcomed. Post them here as comments or send it to my email at cyberfrostprogrammer@yahoo.com.

Thank you for reading.




Happy Coding,
Cyber Frost


Search