My Latest Project
I have been volunteering at ICAF (International Child Art Foundation) since April of this year. When I first started off, I was assigned to be a video editor to edit pre-recorded videos for some international festivals. The reason why I selected this position is probably because I put the video editing skill on my resume
I used Kdenlive and Openshot interchangeably to edit these videos. Sometimes I also used Audacity to sync between left and right channel of the videos.
After three months of video editing, I was just beginning to love to edit videos. However, I assigned to redesign the gallery page on the ICAF website two weeks ago. This is my second web development challenge. My first challenge, as you can see on my website, was to build the first website for another non-profit organization. Although that project was abandoned long time ago because the interruption of school work, I learned the existence of server-side scripting at the time and wrote the first PHP application, the contact form on my website. This time I need to place thousands of children’s artworks and some other pictures to the gallery page in an organized layout.
I know that I have to use pagination for this project. It’s my first time to create pagination pages using PHP. The original design shown below was using JqPageFlow jQuery plugin to implement the pagination as the user scroll down the page.
After some more research, I thought that the infinite scrolling is not good for bookmarking since the manager specifically pointed out that he wants to have a back button for each page. Therefore I revised the design to use the traditional pagination method similar to Google:
Based on the above design, I wrote a test page to test my pagination code. This page is still under development. I will integrate the code into the original page once I completed the testing stage. You can download the source code for this page here (I didn’t include the image files since these images are over 1.5 GB). Note that I used exec PHP function to execute ‘ls’ Linux command to output the image file names to an array rather than use a loop or a database table to process file names. This page also contains a script to split different parts of the file name since these names followed a specific pattern: “country-firstNam_lastName-age-thumb.png” as well as some knowledge I got from the C for loop tutorial. Also note that this is the test code, meaning that I haven’t added extensive comments to the code yet.
C Advanced Operators
There are some advanced operators in C. First set of these operators is the Assignment Operators. Here are five operators presented in the arithmetic assignment operators:
- += (Plus Equals)
- -= (Minus Equals)
- *= (Multiply Equals)
- /= (Divide by Equals)
- %= (Modular Equals)
For example, x += y – 3 is the same as x = x + y – 3.
Other assignment operators that are not commonly used are:
- &&= (And Equals)
- ||= (Or Equals)
- &= (Bitwise And Equals)
- |= (Bitwise Or Equals)
- ^= (Bitwise Not Equals)
- >>= (Bitwise Shift Right Equals)
- <<= (Bitwise Shift Left Equals)
Here is the sample code for the arithmetic assignment operators:
/*
*
* assignment.c
*
* Program to demonstrate the use of assignment operators
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int x, y;
y = 3;
x = 100;
x /= y - 1;Â Â Â // It's the same as x = x / (y - 1), therefore it can substitute to x = 100 / (3 - 1).
printf("x = %dn", x);Â Â Â // The answer is x = 50.
fflush(stdin);
getchar();
}
C String and Character Functions Samples
Here are the exercises I did for the string and character functions:
/* * * exercise1.c * * Allow user to input a string from the keyboard and * display the ASCII value for each character * * by Robby Chen, 2010 * */ #include <stdio.h> main() { char string[51]; int character; puts(""); // Prints a new line fputs("Please enter a string (less than 50 characters, including spaces): ", stdout); // Does not add the new line at the end fgets(string, 51, stdin); puts(""); puts("Character ASCII Value"); puts("-------------------------"); for (character = 0; string[character] != ''; character++) { if (string[character] == 'n') { // prints the new line character and its ASCII code printf("Â Â Â \n%12dn", string[character]); } else { // prints individual letters of the string and their ASCII codes printf("%5c %12dn", string[character], string[character]); } puts("-------------------------"); } puts(""); fflush(stdin); }
Note that the above code contains ASCII-related technique. You can read more about ASCII characters set here if you haven’t already done so above.
/*
*
* exercise2.c
*
* Allow users to enter their names from the keyboard
* and output the names with upper case on the first letter
* and lower case on the other letters of each word
*
* by Robby Chen, 2010.
*
*/
#include <stdio.h>
#include <ctype.h>
main() {
char name[51];
int letter;
fputs("Please enter your name: ", stdout);
fgets(name, 51, stdin);
fputs("Hello, ", stdout);
for (letter = 0; name[letter] != ''; letter++) {
if (letter == 0 || isspace(name[letter - 1])) {
name[letter] = toupper(name[letter]);
printf("%c", name[letter]);
}
else {
name[letter] = tolower(name[letter]);
printf("%c", name[letter]);
}
}
fflush(stdin);
}
C Character Functions
There are several types of character functions in the standard C library. One type of character functions is character classification functions. They are isuppper, islower, isdigit, isspace, and other boolean functions. For example, if (isupper(ch)) determines whether ch is upper case or not.
Another type of character functions is character conversion functions. They are toupper, tolower, and others. For example, ch = toupper(ch) converts ch to upper case whether the original is lower case or upper case.
Here is the same code for the character functions:
/*
*
* ctype.c
*
* Program to show examples of the use of isupper, toupper, etc.
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <ctype.h>
main() {
char ch;
printf("Please type in a character (enter q to quite): ");
scanf("%c", &ch);
while (ch != 'q') {
if (isupper(ch)) {
printf("This is an uppercase charactern");
ch = tolower(ch);
printf("Lowercase equivalent is '%c'n", ch);
}
else if (islower(ch))
printf("This is an lowercase charactern");
else if (isdigit(ch))
printf("This is an digitn");
else if (isspace(ch))
printf("This is an space-type charn");
else
printf("This is an unknown charn");
printf("Please type a character (enter q to quite): ");
while (getchar() != 'n' && getchar() != EOF) {}
scanf("%c", &ch);
}
fflush(stdin);
}
Play Around with Google Nexus One
I received my Nexus One today which I ordered from Google on Saturday. The FedEx delivery of the device was earlier than I expected. They delivered my order 10 AM in the morning, nearly 5 hours earlier than the estimated time of 3 PM.
After I set up the phone, I decided to move the SIM card from my old T-Mobile based cell phone to Nexus One. It only adds basic call functionality to the device since I own the pre-paid SIM card.
GPS on the Nexus One doesn’t require Internet connection. However, the maps must be cached on the device in order to use the full GPS function. Turn-by-turn direction also needs to be cached from the Internet. I have not found any GPS app that supports offline viewing of entire US map that supports different zoom levels from Android Market. Maybe I will create one for myself using OpenStreetMap once I learn the Android SDK
But first I must finish learning C.
The 5MP camera works very well. Below is a test photo that I took in the bathroom.
This photo was taken in front of mirror
Click the image to enlarge
As you can see, the picture is very smooth without flashlight. I have not tested the flashlight and video recorder yet, but I think it won’t be very bad. One thing I noticed when playing around with camera is that the zoom level is not great. For the most time, I got pixelated pictures with the max zoom level. However, the problem isn’t visible on the device until I see the pictures on the PC.
Since I have no idea which audio/video formats does Android support, I copied one mp4 and one avi videos to the root of the memory. The gallery only showed the mp4 video. After browsing through Android Market, I downloaded and tried the ad version of RockPlayer. This player supports DivX, AVI, MKV, and Real formats. I watched some avi videos using this app and they all worked fine.
I transferred all the stuffs from iPod Touch and cell phone to Nexus One. From now on, I only need to carry one device in my pocket instead of two devices. I will see how long will Google Nexus One last in my pocket because my pockets usually get water when it’s raining whether I wear different cloths
More C Strings Input and Output Functions
For the input function, the gets function is much more simpler to use than the scanf function. The scanf function is often used in the advanced C programs. The same is for the output function. the puts function is more simpler to use than the printf function. Here are the definitions for these functions:
- puts – Put String
- gets – Get String
- fgets – File Get String
As you can see in the above definitions, the fgets function is used to get the value from a file. However, fgets is also often used in place of the gets function because gets is mostly misused by programmers.
Here is the sample code for these functions:
/*
*
* string.c
*
* Program to illustrate the use of gets, puts and fgets
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <string.h>
main() {
char name[21];
puts("This is a basic programn");
fputs("Please enter your full name (20 characters max): ", stdout);
fgets(name, 21, stdin);
fputs("Your full name is ", stdout);
puts(name);
fflush(stdin);
if (strlen(name) >= 20)
while (getchar() != 'n' && getchar() != EOF) {}
getchar();
}
Note that when I used GCC to compile the above code with gets function, the GCC compiler would give me a warning stated that the gets function is dangerous and should not use it. Therefore I recommend to use fgets to make things easier.
Also notice that the above code contains a new function, fputs. As my current knowledge, the difference between fputs and puts is that puts automatically adds a break (n) at the end of each output, whereas fputs does not. The fputs function is good to display the prompt for user to enter the data.
Finally Ordered Nexus One (Updated)
Update: I received the phone. Please read this post for more information on my first hands-on of Nexus One.
After I heard this morning that Google will stop selling its Nexus One once the final set of devices is sold out, I decided to order unlocked one from the Nexus One web store.
When Google first released Nexus One, I was hoping that the original version of Android could sometimes come to some media players similar to the iPod Touch. Back then, I didn’t know the meaning of unlocked phone. I thought it is some kind of hacks that allows phone to be used illegally.
As time went by, I discovered Zii Egg. As some of you who read the posts during the early stage of this blog may know, I was waiting for Zii Egg to be released as a consumer product for over five months. Within these months, I learned a lot of how the Android operates and different variations of Android. I also discovered the definition of unlocked phone.
Since the unlocked version of Nexus One requires a SIM card and it has wifi functionality, and I don’t use cell phone very much, I could use Nexus One without the SIM card as a PMP (Portable Media Player) device. This idea came to my mind last week.
Since then, I have abandoned waiting for the Zii Egg. Mainly because I want the Android market on the device, and as far as I know, only the original version of Android has this feature. Because of the financial issue and the surprise of the unlocked Nexus One being too expensive, I couldn’t order the device immediately. However, after today’s news, I finally decided to purchase the device using my remaining funds.
According to the tracking info on FedEx, I will receive the device by next Monday at 3:00 PM EDT. I will post an update once I received the device and test it whether it supports operation under none-SIM card condition, as well as some experience with the device as appropriate.
C Arrays
An array is a variable that represents a collection of the same variable types. For instance, a collection of doubles is an array. It can also declare one variable as an array. The size of an array depend on the memory size of the target computer. It can store larger array size if the memory size is bigger.
Below is the arrays declaration format:
typename arrayname[init_literal];
As you remember from previous posts, this format is the same as when declaring strings. A string is a collection of characters, which also is an array.
After an array was declared, each element in the array can be accessed individually. For example:
floatarray[5] = 3.56;
Like other programming languages, the first element of an array in C always starts with 0. Therefore for the above example, floatarray[5] refers to the sixth element of the array.
Arrays are often used with loops to operate with all elements sequentially, especially the for loop. When used inside a loop, an array of n elements always stars from 0 to n – 1. For instance, an array of 10 elements would start from 0 to 9 inside a loop.
Here is the sample code for the arrays:
/*
*
* array.c
*
* Program to illustrate the use of arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
float array[5];
int i, index;
for (i = 0; i < 5; ++i) {
printf("Please enter element number %d: ", i + 1);
scanf("%f", &array[i]);
while (getchar() != 'n' && getchar() != EOF)
printf("");
}
printf("Which element do you want to see? ");
scanf("%d", &index);
while (getchar() != 'n' && getchar() != EOF)
printf("");
printf("Element %d is %fn", index, array[index-1]);
fflush(stdin);
getchar();
}
The following sample code demonstrates how to use strings with arrays:
/*
*
* string.c
*
* Program to illustrate the makeup of a string
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[51];
int length;
printf("Please enter your name: ");
scanf("%[^n]", name);
while (getchar() != 'n' && getchar() != EOF)
printf("");
for (length = 0; name[length] != ''; length++) {}Â Â Â // do nothing
printf("The length of >>>%s<<< is %dn", name, length);
fflush(stdin);
getchar();
}
Notice the name[length] != ” statement. The slash zero inside the quotes represents a NULL character. It means that the loop will end if there is no more character to read.
Multi-dimensional Arrays
A multi-dimensional Array is an array made of multiple arrays. Here is an example:
/*
*
* multi.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int grid[8][3];Â Â Â // It contains 8 arrays and each array has 3 elements
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
grid[i][j] = (i + 1) * (j + 1);
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
printf("%3d ", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is the sample code for the string usage in a multi-dimensional array:
/*
*
* multi2.c
*
* Program to demonstrate multi-dimensional arrays
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char strings[5][20];Â Â Â // It contains 5 strings and each has 20 characters long
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
while (getchar() != 'n' && getchar() != EOF) {}
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
fflush(stdin);
getchar();
}
Array Initialization
Arrays can be initialized at the beginning of the program when they are declared. During the initialization, it’s unneccessay to specify the size of the array. For instance, here is the declaration of an array:
int array[40];
And here is the initialization of an array when it is declared:
int array[] = {3, 6, 9, 12};
Note that it’s also possible to initialize the array when the size of the array is specified, but as stated above, it’s not necessary. For example:
int array[10] = {3, 6, 9, 12};
In the above example, the first four elements in the array got initialized, while the rest of the elements were set to 0.
Here is the sample code to show how the multi-dimensional arrays are initialized:
/*
*
* multi3.c
*
* Program to demonstrate initializing multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char grid[8][8] =
{
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'}
};
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is for the multi-dimensional arrays for strings:
/*
*
* multi4.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[50] = "Mark Virtue";
char strings[][20] = {"This", "is", "how", "to", "initialize"};
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
}
Note that strings in the multi-dimensional arrays can only have one set of arrays that has non-specified size, notably the arrays in the first dimension. The size of arrays in other dimensions needs to be specified.
C Special Loops
There are two special loops in C:
- Nested loop – a loop inside of another loop, also called a loop within a loop.
- The forever loop – a controlled infinite loop
Here is a sample code for the nested loop:
/*
*
* nested.c
* Program to demonstrate the use of nested loops
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int i, j;
for (i = 1; i <= 5; i = i + 1)
for (j = 1; j <= 3; j = j + 1)Â Â Â // nested loop
printf("Here we are in the inner loop with i = %d and j = %dn", i, j);
fflush(stdin);
getchar();
}
Nested loops are often used in the multi-dimensional arrays.
And here is the forever loop:
/*
*
* forever.c
* Program to demonstrate the use of the "forever" loop
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char type;
int album;Â Â Â // boolean
for (;;) {Â Â Â // A for loop that has no arguments will repeat the loop forever
printf("Album or single (a for album, s for single)? ");
scanf("%c", &type);
while (getchar() != 'n' && getchar() != EOF)Â Â Â // nested loop
printf("");
if (type == 'a' || type == 's')
break;
printf("Errorn");
}
album = type == 'a';
if (album)
printf("Albumn");
else
printf("Singlen");
fflush(stdin);
getchar();
}
The forever loops can only used in the for loops, and it’s similar to the while loop for input validation.

