Sunday, 25 November 2012 0 comments

Week 9-11: Arrays, Pointers and Strings

As near to the end of the course, it leads to more difficult and complicate content. We have gone through several chapters about Arrays, Pointers and Strings. An array is a fixed-size, sequenced collection of storage elements of the same data type. It likes a cabinet containing many drawers. Each drawer stores one value. We can easily refer to different drawers to get the corresponding value stored, it helps to simplify our program.

An array can be defined to be different dimensional. Below is an typical example of using 1-D array.


Sorting is an application using array, it means to re-arrange a list of items so that they appear in increasing (or decreasing) order. One of the most simple sorting algorithms is the bubble sort, which uses the Archimedes Principle: Lighter materials should rise to rest on top of heavier materials.



One of the most important topic what I think in this course is Pointer. It is very useful in programming, through pointers, we can directly access/modify the data stored in the memory. Pointers are usually related to the arrays, an array variable is actually a pointer variable storing the base address of the array. Therefore, a pointer variable can be used as if  the pointer variable is an array. An array is a pointer, but a pointer is not necessarily an array.

Another topic that related to Pointers and Arrays is Strings. A string is a sequence of characters.  It is a kind of data. A string is stored as an array of char. Strings are often processed using pointers and the C standard library provides many useful string handling functions. The following is an example using pointers, arrays and strings. By using these, the whole program is simplified and its content can be easily modified.


To apply these practically, I used them in assignment 4. This program simulates a three-player popular board game of Aero-fighter in a simple version. Each player has one airplane only and the passage for the three players is the same – a single line with 63 slots on it: one Airport, one Start, 60 slots colored with red, green, blue in repeated successive order, and finally one Terminal. The game ends once a fighter steps on (or pass through) the Terminal slot. The die value for each roll is displayed for every player on the first line of the program. Upon ending the game, the screen will display a message at the bottom of the screen showing which player wins and the program should end its execution.

Wednesday, 7 November 2012 0 comments

Week 6-8: Miscellaneous Topics

In the previous week, I have submitted the third assignment in this course. This program acts as a calendar generator. User can enter the number of a year from 1990 to 2100 (inclusive) and desired month from 1 to 12 (inclusive) via the keyboard. The program will print the calendar according to user's input. For each month, it provides a heading showing the month and year, plus headings for the days of the week. Below is the programming result.


In the tutorial of assignment 3, two methods are provided for reference. I chose method B, using Zeller's congruence for the Gregorian calendar to calculate the day of the week in the program.


For the lecture, we have gone through some miscellaneous topics, including Fundamental Data Types, Functions, Variable Scope and Storage Class, Array Processing, Pointers and Arrays Revisited. Most of these topics are new to me, so I would like share two easier topics, Fundamental Data Types and Functions, in detail in this post.

At the beginning stage, we mainly used char, int and float in writing programs. In this chapter, we learnt all fundamental data types as shown below. They are classified into two types, Integral Types and Floating Types. For the presence of Integral types and Floating types in the same time, this is called Arithmetic Types.


Besides, the 7-bit ASCII Table has been introduced. ASCII stands "American Standard 
Code for Information Interchange". It includes some printable characters: 'A', '=', ':', ... and some non-printable characters: newline (NL), bell (BEL), tab (HT, VT), ... ASCII indicates the different values stored in one byte of each character. One byte equals 8 bits, which has 2 to the power 8 possibilities, 256 distinct values can be represented. Therefore, the full range of value is 0 - 255, ASCII uses 0 - 127 only.


For some non-printing or hard-to-print characters in ASCII, we write them with escape character (\) in C, instead of typing them directly, and it is called escape sequence.


On the other hand, I have learnt an interesting topic, Function. To use it, we need to define a function then we can call and execute it afterwards. It is very useful in writing programs, as illustrated below, the program can be simplified by calling a function, instead of using printf("*****************\n") one more time.


The following is another example similar to the above one, we can divided the function helping us to count the number of symbols we want to print out. By this method, we can save our time in counting the number of symbols and counting error can be prevented.


Apart from this, there are many different types of function, below are the examples of Math function. They help us to perform mathematical functions such as exponential function, absolute value, and even logarithm an d trigonometric functions.