• missing xbfish.com image

Category Archives: Programming

[Linux] Check Ruby and Rubygems version

To check Ruby version installed on a Linux, enter the following command in Terminal:

ruby -v

To check for Rubygems version, the command is:

gem -v

Install Ruby 1.9.2 in Ubuntu

To keep this post sweet and concise, the first thing you need to do is to open up Terminal.

Gain root access by entering the following:

sudo -s

You will be prompted for your root password.

Next, enter the followings:

apt-get install build-essential zlib1g zlib1g-dev libruby1.9.1 libxml2 libxml2-dev libxslt1-dev
apt-get build-dep ruby1.9.1
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p0.tar.bz2
tar xvjf ruby-1.9.2-p0.tar.bz2

After extracting, open up ruby-1.9.2-p0/ext/Setup and uncomment zlib line (remove the #).

Then, configure, compile and install:

./configure
make
make install

Viola!~ You are done installing Ruby 1.9.2

HTML5 doctype tag

With the introduction of HTML5, the doctype is made simpler. The doctype for HTML5 is as follows:

<!DOCTYPE html>

Example of a html structure for HTML5:

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
 
<body>
The content of the document......
</body>
 
</html>

[WordPress] Remove Twenty-Eleven Theme mobile formatting

Twenty-Eleven theme is WordPress version 3.x default theme and it supports mobile format. This means whenever a user browses a WordPress powered site using his/her mobile phone, the site will be displayed in a mobile friendly way.

If you want to remove this mobile formatting, open up header.php in Twenty-Eleven Theme folder. Remove/comment the following line:

<meta name="viewport" content="width=device-width" />

Hope this helps. :)

Insertion Sort in C#

Insertion sort is an algorithm of sorting an array whereby elements are compared against and inserted towards the left position.

The insertion sort (ascending to descending) code in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int [] array =  new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
        int array_size = 10;
 
        Console.WriteLine("The array before Insertion Sort is: ");
        for (int i = 0; i < array_size; i++){
            Console.WriteLine("array[" +i +"] = " +array[i]);
        }
 
        // Now we will use Insertion sort
        int temp, k;
        for (int i = 1; i < array_size; i++) {
            temp = array[i];
            k = i - 1;
            while (k >= 0 && array[k] > temp) {
                array[k + 1] = array[k];
                k--;
            }
            array[k + 1] = temp;
        }
 
        Console.WriteLine();
        Console.WriteLine("The array after Insertion Sort is: ");
        for (int i = 0; i < array_size; i++) {
            Console.WriteLine("array[" + i + "] = " + array[i]);
        } 
    }
}

Here is a dancing video that illustrates insertion sort:

Bubble Sort in C#

Bubble sort is an algorithm of sorting an array whereby larger elements will be push to the back of the array.

The bubble sort (ascending to descending) code in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int [] array =  new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
        int array_size = 10;
 
        Console.WriteLine("The array before Bubble Sort is: ");
        for (int i = 0; i < array_size; i++){
            Console.WriteLine("array[" +i +"] = " +array[i]);
        } 
 
        // Now we will use bubble sort
        int temp;
        for (int index = array_size - 2; index >= 0; index--) {
            for (int i = 0; i <= index; i++) {
                if (array[i] > array[i + 1]) {
                    temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
 
        Console.WriteLine();
        Console.WriteLine("The array after Bubble Sort is: ");
        for (int i = 0; i < array_size; i++) {
            Console.WriteLine("array[" + i + "] = " + array[i]);
        } 
    }
}

Here is a dancing video illustrating bubble sort:

Selection Sort in C#

Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.

The selection sort (ascending to descending) code in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int array_size = 10;
        int [] array =  new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
 
 
        Console.WriteLine("The array before Selection Sort is: ");
        for (int i = 0; i < array_size; i++){
            Console.WriteLine("array[" +i +"] = " +array[i]);
        }
 
        // Now we will use selection sort
        int tmp, min_key;
 
        for (int j = 0; j < array_size - 1; j++){
            min_key = j;
 
            for (int k = j + 1; k < array_size; k++){
                if (array[k] < array[min_key]){
                    min_key = k;
                }
            }
 
            tmp = array[min_key];
            array[min_key] = array[j];
            array[j] = tmp;
        }
 
        Console.WriteLine("The array after Selection Sort is: ");
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("array[" +i +"] = " +array[i]);
        }
    }
}

To compliment the above code, here is a dancing video to illustrate selection sort:

Difference between Parameters & Arguments

Paramters:
A parameter represents a value that the procedure expects you to pass when you call it. You can also indicate that a parameter is optional, meaning the calling code does not have to pass a value for it.

Arguments:
An argument represents the value you pass to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure. Each argument corresponds to the parameter in the same position in the list.

Extracted from: http://msdn.microsoft.com/en-us/library/9kewt1b3%28v=vs.80%29.aspx

Compiling C programming source files using GCC

Suppose we have these 2 files:
hello.c
bye.c

To compile and link these 2 files in one step through GCC:

gcc -o program hello.c bye.c

The output object will be program. To run the program, enter the following in the command line:

./program

We use the ./ to specify that we are running this file residing at the current folder only.

To compile the 2 files and link it through multiple steps:

gcc -c hello.c
gcc -c bye.c
gcc -o hello.o bye.o

The -c flag tells the compiler to compile only and not call the linker.

Quick Sort vs Bubble Sort

Interesting video!