Sunday 25 November 2012

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.

0 comments:

Post a Comment