• missing xbfish.com image

Category Archives: Programming

Get a single character from string in C programming

Well, this is easy if you are familiar with pointers.

Suppose that you have this code to get the input from stdin stream:

#include <stdio.h>
 
int main(void){
   char destination[20], *ptr, c;
 
   fgets(destination, 20, stdin);
 
   return 0;
}

Next, you want to output each character line by line:

1
2
3
4
5
6
7
ptr = destination; // Have the pointer point to the array destination 
 
while(*ptr != '\0' && *ptr != '\n'){
   c = *ptr;
   printf("%c \n", c);
   ptr++;
}

The output will be each single character being printed out line by line, inclusive of white-space character.

Now, lets see what we have done over here. At line 3 of the second code snippet, the condition is set to run if *ptr != \0 and \n. Why? This is because a string terminated with a \0 character. As you get the input from stdin stream, fgets included the \n (newline character) when you press Enter key on the stdin stream before appending the \0.

For line 4, we set *ptr to c, a char variable. As ptr is pointing to the address of the string, we de-reference it to get the value. Since variable c is only able to hold a single character and ptr is pointing to a single character, the de-referenced of ptr will be a single character from the string.

Last but not least, we increment ptr to point to the next character of the string.

I hope this is a clear explanation for you. Do leave a comment if you have any problem :)

Tail recursion in Ruby

Under direct recursion, there is 2 main recursion style: 1) Traditional recursion, 2) Tail recursion.

So what are all these?

Take a look at the following example coded in Ruby:

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
def recursion(num, power)
	if power == 1
		return num
	else
		return num * recursion(num, power - 1)
	end
end
 
def tail_recursion(num, power, result)
	if power == 0
		return result
	else
		return tail_recursion(num, power - 1, result * num)
	end
end
 
puts "Please enter the number follow by the power:"
 
result = 1
num = gets.to_i
power = gets.to_i
 
puts "Trad recursion: " +recursion(num, power).to_s
 
puts "Tail recursion: " +tail_recursion(num, power, result).to_s

For recursion(), it is not efficient as the base case is not the last to be execute. The program will navigate to and back through the stack to return the final value.

Example:

recursion(2, 3)
2 * recursion(2, 2)
2 * (2 * recursion(2, 1))
2 * (2 * 2)
2 * (4)
8

As for tail_recursion(), the result will be return when the program reach the base case.

Example:

tail_recursion(2, 3, 1)
tail_recursion(2, 2, 2)
tail_recursion(2, 1, 4)
tail_recursion(2, 0, 8)
8

Include header file in C programming

If you want to use standard header files in C, you will include it like the one shown below:

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

Now lets say you have a self-written header file, called custom.h, you will include it like this:

#include "custom.h"

Stack and Heap in Programming

Stack
The section of memory that is allocated for automatic variables within functions.

Heap
An area of memory used for dynamic memory allocation.

In general, the stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. The heap is the section of computer memory where all the variables created or initialized at runtime are stored.

missing xbfish.com image

Extract from:
http://www.maxi-pedia.com/what+is+heap+and+stack
http://wiki.answers.com/Q/What_is_difference_between_heap_memory_and_stack_memory

Compiling and linking multiple C source files using GCC

Suppose we have 2 sources file in C programming:
hello.c
world.c

To compile and link them together using GCC, type the following command:

gcc -o helloworld hello.c world.c

helloworld will be the compiled output of hello.c and world.c

[Prestashop] Problem uploading product image?

Are you having problem uploading product image in Prestashop like the one shown in the screenshot below?

missing xbfish.com image

If your turn on error reporting in config.inc.php, you will see the following error:

missing xbfish.com image

The reason for this error to pop out is because you are probably uploading images that are way too large. Try resizing the image and upload again.

There should be no problem. :D

Generate random number in C programming

Forget about using rand(), you will find that the random number is always the same when you run your C program.

To achieve ‘real’ random number, make use of srand().

Consider the following example:

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

int main(void){
   srand(time(0));
   printf("First random number: %d\n", rand()%100);
   printf("Second random number: %d\n", rand()%100);

   return 0;
}

When you compile and run the above code, you will find that the random number is really random each time the program is run.

We make use of current time (in seconds) using time(0) as the seed to srand(). As such, each time we call rand(), the number generated by rand() will be completely random.

Greying or Fading image using CSS

CSS is powerful and easy to script. You can grey out or fade an image using CSS by playing around with the opacity.

Example of an image in its original form:

missing xbfish.com image

Now I will set its opacity to 50% using CSS:

.opa_image{
     border:none;
	-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
	filter:alpha(Opacity=50);
	-moz-opacity: 0.5;
	opacity: 0.5;
}

The html code for the image:

<img src="lotus.png" class="opa_image">

The final result of the image after manipulating:

missing xbfish.com image

If you are proficient in CSS scripting, you can certainly add more features like hovering the image to produce the fade effect or whatsoever. Definitely not limited to the example above. :)

CSS Font Cheat Sheet

This cheat sheet is generally for my usage as I often have to search around which common fonts I should use when it comes to web development.

*Example texts below are set to a font-size of 14px.

Sans Serif
Generally best for most text on screen.

  • Trebuchet MS – The world is crashing soon before you could get to the top. 1234567890
  • Verdana – The world is crashing soon before you could get to the top. 1234567890
  • Arial – The world is crashing soon before you could get to the top. 1234567890
  • Helvetica – The world is crashing soon before you could get to the top. 1234567890
  • Tahoma – The world is crashing soon before you could get to the top. 1234567890
  • Geneva – The world is crashing soon before you could get to the top. 1234567890
  • Lucida Sans Unicode – The world is crashing soon before you could get to the top. 1234567890
  • Lucida Grande – The world is crashing soon before you could get to the top. 1234567890
  • MS Sans Serif – The world is crashing soon before you could get to the top. 1234567890

Serif
Generally best for text designed to be printed.

  • Times – The world is crashing soon before you could get to the top. 1234567890
  • Times New Roman – The world is crashing soon before you could get to the top. 1234567890
  • Georgia – The world is crashing soon before you could get to the top. 1234567890
  • Garamond – The world is crashing soon before you could get to the top. 1234567890
  • Palatino Linotype – The world is crashing soon before you could get to the top. 1234567890
  • Palatino – The world is crashing soon before you could get to the top. 1234567890
  • Book Antiqua – The world is crashing soon before you could get to the top. 1234567890
  • New York – The world is crashing soon before you could get to the top. 1234567890

Monospace
Generally best for representing code or typewritten text.

  • Courier – The world is crashing soon before you could get to the top. 1234567890
  • Lucida Console – The world is crashing soon before you could get to the top. 1234567890
  • Courier New – The world is crashing soon before you could get to the top. 1234567890

Almost all of the stuffs here were copied over from WebChicklet. You may also wish to look at the 10 Principles for readable web typography when choosing fonts for your web.

I hope this cheat sheet and link can also be useful for you :D

Pre/Post Increment & Pre/Post Decrement in PHP

Most of the time, I have seen newbie programmers using Post Increment/Decrement in PHP instead of utilizing on Pre Increment/Decrement.

So, here is a simple guide in showing Pre/Post Increment and Decrement.

Pre/Post Increment example:

1
2
3
4
5
$x = 5;
$y = 5;
 
echo "The value of x is: " .$x++; // Post Increment
echo "The value of y is: " .$++y; // Pre Increment

The result:

The value of x is: 5
The value of y is: 6

Notice that y is increment before it was echo out. We termed this as Pre-Increment. For Post-Increment, the value will only +1 after the line is being echo’ed out or being processed by the function it was encapsulated in.

The end result is the same, the value of x and y will be 6.

Pre/Post Decrement example:

1
2
3
4
5
$x = 5;
$y = 5;
 
echo "The value of x is: " .$x--; // Post Decrement 
echo "The value of y is: " .$--y; // Pre Decrement

The result:

The value of x is: 5
The value of y is: 4

The end result for both the value of x and y will be 4.

In a nutshell, Pre/Post increment or decrement allows you to specify whether you want to increment/decrement before the line of code is being executed or after the line has executed.