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.

Tuesday, 16 October 2012 0 comments

Week 4-5: Flow Control

In the first few weeks, I had a rough view of the basic syntax and structure of C language. Instead of writing programs practically, I learnt to read a program which mainly involving printf() statements and some simple syntax, and I got 100 points in the first assignment!

In the previous two weeks, a new field - Flow Control was introduced. It was divided into two parts, in Flow Control I, relational, equality and logical operators were introduced. They are provided to facilitate flow controls, especially in calculations and executing statements, it is important to distinguish their meanings and purposes. Besides, two types of flow controls, sequential and selection, were taught. For sequential, statements in a program are executed one after another, it is easy to understand and use. For selection, it involves a choice of alternative actions, and can be classified into if, if-else, and switch. When using selection, statements will be executed in different orders depending on the value (zero or non-zero) of the expression. I have learnt the if and if-else statement in cert. level so a clear concept has already existed in my mind.


In programming, we should be very careful about the structure, the order of each word, and the presence of each symbol. The following are four modifications of a program, they are very alike, but actually one of them is different from the others.


 In Flow Control II, repetition was introduced. In this type of flow control, we use while and do-while statements to repeatedly execute a statement. They are very useful in performing miscellaneous calculations and tasks. In the following example, for statement, which is equivalent to the while statement, is used to find the factorial of an input integer.


To practice, assignment 2 was finished in the last week and it was required to use prinf() statements, print-out (screen) formatting and branching statement "if-else" to perform simple calculation using C. It was the first program I wrote, it behaves as a price comparator for mobile phone users. The program first prints the information of mobile phone network operator monthly plans, it then asks for the air-time usage information. After inputting a single integral number, it is expected that the costs of joining different operators and different plans will be calculated and listed out. The flow control in this program is not complicated and is rather easy, so I spent only about 45 minutes in compiling it.



In short, Flow Control is a challenging topic and it consolidates my mind in writing programs. A program involving Flow Control will not be workable if we just follow the structure and put in all the syntax validly. Addition to syntactically correct, it also requires plentiful logical thinking. We need to design every statement logically with our problem-solving skills, or else, the program will be resulted in a valid meaningless one, infinite loop is a good illustration.
Wednesday, 26 September 2012 1 comments

Week 1-3: Basis of C Language

To me, a freshman at university, everything is new and fresh. In the first semester, CSCI1110 is undoubtedly the course I most looking forward to study. Believe it or not, I love computers. Choosing between Information Engineering and Computer Science has already driven me crazy, imagine how excited I am when  I can study Computer Science course as a student of Information Engineering.

Programming is a new field to me, absolutely. Although I have studied Computer & Internet Technology in cert. level, I chose multimedia stream at that time. Multimedia and HTML can be easily accessed in our daily computer uses, I learnt web authoring by myself  since I was still in primary school. However, we cannot easily gain knowledge about programming when using computers. Now I have the chance to make friend with programming!

The lecture courses of CSCI1110, out of my expectation, is not that interesting. Hmmm... maybe this is because of the  beginner level, what we have learnt are mostly theoretic stuff but not so practical. Nevertheless, those knowledge is very important for me to build a clear concept of computing in my mind. I have learnt the syntax,expression, structure, elements,... of a high level language: C language. It is told that C language is small and relatively easy to learn, and I quite enjoy attending the lessons. I have just learnt different operators, prefix and postfix forms, and field-widths control in the previous week. I found that they are actually like maths, but involve some special rules which are just for programming, it is quite interesting and I should pay attention to them to avoid mixing up with maths. 


The professor has suggested some reference book, or called textbooks for us, I visited CUHK Library and a public library after class to find them but failed. Finally, I have borrowed a reference book written in Chinese, surely for Chinese to read and understand, so I think it will be suitable for me as a beginner to learn.

This week, the first assignment in CSCI, and even my university studies, is going to be handed in! I can finally put what I have gained in the lesson into a practical way. The assignment consists of two parts: This first part is a typing exercise, we just need to type the given program which is shown in image form into Visual C++ 2010 Express; the second part is to describe what this program does.


This assignment aims at helping us to get acquaint with the Visual Studio/ C++ programming environment. I have tried Visual C++ 2010 Express, it is definitely a new toy for me, HAHA! 



Below is a part of my typing work, strictly followed the given sample. Hope that there is no typing mistakes.


This assignment requires us to try to compile and execute the program with appropriate arguments. I have succeeded!