The only way to attain proficiency in programming is to practise. This includes trying out and experimenting with the programs presented in the lectures, as well as the tutorials, and programming exercises. It is not good enough just to read C code. You must type it in, compile it, and run it! Your programming aptitude can only be improved with programming experience. As you will be using a UNIX platform for C programming, we have provided a step-by-step walkthrough which will guide you through the following:
You need to be very familiar programming on a UNIX platform as you will be required to program on our workstations during your weekly labs.
We shall use a portable version of Cygwin (with CourseMarker) which is a UNIX-like environment and command-line interface. You can either install on your hard-drive, or on a portable thumb-drive or external hard-disk so that you can plug-and-play on any machine to do your work. The downside of installing on portable drives will be the slightly slower processing speed, so you will need to decide on which is more of a primary concern: to do things fast, or to do things anywhere you like. We suggest a minimum disk space of 512MB for the installation. Cygwin need not be the sole application on your storage device, so you can install together with your favourite media files on your media players, provided that these are visible as removable storage devices on the machines to which they are plugged in.
The detailed setup instructions for Windows and Mac machines are given below. Linux/FreeBSD users should be competent enough to perform the proper set up; just take the Mac instructions as a guide.
Download cygwin.zip and extract the contents onto your portable drive or hard-disk. You are advised to extract to the root directory of a drive (e.g. C:\, D:\, F:\). Navigate to your drive and/or folder where the files were extracted, and you will see four files.
If everything goes well, you will see the following Cygwin interactive window.
For Windows Vista users, you might encounter a "Permission Denied" error with regards to "umount". This is an issue with Vista's UAC (User Account Control) . The best bet is to invoke Cygwin with administrator privileges by right-clicking on "cygwin.bat" and select "Run as administrator".
Next, check if the "gcc" C program compiler exists by entering the command "gcc --version". If a "command not found" message is output, then you will need to install Xcode. You can install Xcode either directly from the Installation DVD that comes with your Mac, or you can create an online Apple Developer Connection (ADC) account from http://developer.apple.com/mac/. Once your ADC account is created, you may login, then download and install Xcode.
To set up CourseMarker submission, double click "cygwin.command". Doing this will also enable files to be properly stored in the portable drive so that you can plug it into another Windows machine to continue your work. You will see the following Terminal session.
Instead of double clicking "cygwin.command", you can enter the command (with its associated path) directly from within the usual Terminal session. The correct command to enter is shown when you performed the double click earlier. From the preceding example, notice that the command is "/Volumes/SOC\ CYGWIN/cygwin.command".
Whichever method you use, as long as you see the local UNIX prompt of the form "localuser@localmachine:~[xxx]$" , then you would have successfully performed the necessary setup on your local machine. Note that localuser and localmachine varies with different users and machines, and xxx is a running number. You are now at the home directory level of your very own UNIX platform. All the common UNIX commands can now be used. To exit, do not close the window by clicking on × at the top corner. Just enter the command "exit". You may also open multiple windows, if necessary, by repeatedly clicking on "cygwin.bat" or "cygwin.command".
In your weekly lab sessions, you will be requested to login to our UNIX workstations to do your work. There are two separate workstations: sunfire and plab. Sunfire is accessible round the clock, while plab is dedicated only for sit-in labs and practical examinations. If you are accessing sunfire for the first time, you will need to create (or re-enable) your own personalized sunfire account. Start up a web browser, and click https://mysoc.nus.edu.sg/~newacct to create your UNIX Account (use a maximum of 8 characters in your password or you may encounter problems). You must do this before you are able to login to your UNIX account. You need to do this only once for the whole semester.
You can login to sunfire from the UNIX platform of your local machine. Assuming that your userid in sunfire is u0912345, enter the command (in bold) at the local UNIX prompt.
localuser@localmachine:~[xxx]$ ssh u0912345@sunfire-r.comp.nus.edu.sgWhen you are prompted for "Password", type your password slowly and carefully, then press the <ENTER> key. The password is case-sensitive and for security reasons, you will not get to see what you have typed. Once connected you will see the following. Notice that the UNIX prompt has changed to "u0912345@sunfire0:~[xxx]$" indicating a successful login. You are now at the home directly level of your UNIX account in sunfire.
Connection problems
If you have problems connecting to sunfire-r.comp.nus.edu.sg, then most likely you are connecting from outside the NUS SoC network domain. You will need to activate SoC-VPN. You should use your NUSNET account id and password to login to SoC-VPN. Refer to https://noc.comp.nus.edu.sg/docs/using-socvpn for information on how to install and activate SoC-VPN for your specific machine. Note that for Windows Vista users, you have to right click on "Connect to SoC-VPN" and choose "Run as Administrator".
User-Ids and Passwords
When you have logged in successfully, you will be immediately asked if you would like to change your password. Enter 'n' (no) to skip it. Do NOT change your password as you are still new to the system and if you make any mistake, time may be wasted as you need to get it rectified at the Technical Services Centre (located on ground floor of COM1).
You may want to change your password some other time, after you have thought of a good password. Choose a password that is 6 to 8 characters long. There are guidelines to choosing good passwords which are safer and less susceptible to break-ins. When you change your password, the system will check that your new password satisfies its stringent requirements, failing which it will reject the change. Generally, the system tends to accept passwords that consist of at least a mix of uppercase and lowercase characters, plus some digits. It is advisable that you prepare a few passwords before-hand, so that if one fails to pass the test, you can pick another without wasting too much time. (Refer to this web page to find out more after this lab.) You can change your password any time you like -- in fact, you are advised to change it regularly. The command to change password is "passwd". It will take half an hour to one hour before your new password takes effect.
Always remember your password! If you forget, you would need to approach the personnel at the Technical Services Centre to submit a request for a new password. Alternatively, you can click HERE to change your password online (this requires your NUSNET ID and password).
For your weekly labs, you will use the SSH Secure Shell to login to sunfire (or plab). The PCs in the labs comes with SSH Secure Shell installed.
Using the PCs in the lab
You should be greeted with a Windows screen. (If the PC is not on, switch it on.) As instructed on the screen, press Ctrl-Alt-Delete.
A login screen appears. You are to type in your NUSNET user-id, your NUSNET password, and select the NUSSTU domain. For students registered this year, your NUSNET user-id begins with u09 followed by 5 digits (example: u0912345, note that it is a small letter u), this is different from your matric number which has less digits and a check character at the back (example: U091234A, note that it is a capital letter U).
Note: All NUS students are issued with the NUSNET account by the Computer Centre. The domain name of this account is "@nus.edu.sg". All SoC students, as well as students from other faculties taking SoC modules, are also issued with SoC UNIX account. The domain name of the SoC account is "@comp.nus.edu.sg". This SoC UNIX account will be purged at the end of the semester for non-SoC students. Do not mix up these two accounts.
Left mouse click on the Start button at the bottom-left corner, select All Programs, look for SSH Secure Shell folder, and left click on Secure Shell Client. A screen similar to the one shown below will appear. Click on Quick Connect button and a small window Connect to Remote Host will pop out.
Under Host Name, enter sunfire-r.comp.nus.edu.sg (works from within NUS or from any of the local ISPs, e.g. SingNet, StarHub, PacNet). Please ensure that you have already created your UNIX Account as described above. Your UNIX Account is not the same as your NUSNET Account.
Under User Name, enter your SoC user-id. UNIX is case sensitive, so you must type "u09" and not "U09".
Ensure that the Port Number is 22 and click on the Connect button.
If you encounter the following window, just click "Yes".
When this is done, another small window will appear to prompt you for your password. Note that the user-id and password are case-sensitive (i.e. an "a" is different from an "A"). The password will appear on the screen as asterisks (***) so you need to type slowly and carefully. Once you have logged into sunfire, you are automatically placed in your home directory.
To use SSH from your own Windows PC/notebook, you can download and install SSHSecureShellClient-3.2.9.exe which is available from the Miscellaneous folder in the IVLE Workbin.
For this course, you will also need to setup your sunfire account. This step must be correctly done or else you will NOT be able to submit your programs for marking!
Run the setup program as follows (you should do this only ONCE). Type the following commands in your SSH Secure Shell window:
~cs1101c/lab0/setup
Check that you enter the above command ACCURATELY (the tilde character ~ is usually found on the top left of your keyboard next to the "1" key). This command initializes your account and prepares it for this initial lab session. It creates the "c" and "doc" directories (a directory is like a folder), and copies some files into your account (so that you don't have to type them from scratch). Refer to Appendix A for a list of these files and their descriptions.
After the setup program runs successfully, you must type:
source .bash_profile
This sets up useful commands for your program submission later. Remember to type the SPACE in between "source" and ".bash_profile". "Respect the spaces". Note that if the above command ran successfully, you will see no output before the next prompt appears. With many UNIX commands, "no news is good news". This is very different from Windows or Mac where you are used to seeing a dialog box pop out. Try to get used to UNIX quickly.
Whether you are using UNIX on your local machine or logging into sunfire, now is the time to start your UNIX adventure. The power of UNIX stems from the many commands it offers. The following are a few commonly used commands. This list is by no means exhaustive and you are urged to explore on your own. Note that UNIX commands are case-sensitive.
In the examples below, bold words are commands which you are expected to enter. All commands are to be entered after the UNIX (local or sunfire) prompt of the form
user@machine:~[xxx]$~ indicates that you are currently in your home directory. The following examples assumes that user u0912345 is logged into sunfire; however you can do it on your local UNIX platform too.
| pwd | to Print current Working Directory to show you which directory you are currently in |
u0912345@sunfire0:~[xxx]$ pwd /home/u09/u0912345 | |
| ls | to LiSt files in your current directory |
u0912345@sunfire0:~[xxx]$ ls c doc | |
| You may also use "ls -F" for more information (-F is one of the many options/flags available for the ls command. To see a complete list of the options, refer to the man pages, ie. "man ls".) | |
u0912345@sunfire0:~[xxx]$ ls -F c/ doc/ | |
| The slash (/) beside the filename tells you that the file is a directory (folder). A normal file does not have a slash (/) beside its name when "ls -F" is used. | |
| You may also use the "ls -l" command (dash L) to display almost all the file information, include the size of the file and the date of modification. Try it now! | |
| cd | to Change Directory from current directory to another |
u0912345@sunfire0:~[xxx]$ cd c u0912345@sunfire0:~/c[xxx]$ ls -F ch1_1.c ch2_1.c ch2_2.c ch2_3.c ch2_4.c | |
| Note that the prompt changes to ~/c to indicate that you are
now in the c directory below your HOME directory.
Entering "cd" alone brings you back to your HOME directory, ie. the directory in which you started with when you first logged into the system. | |
u0912345@sunfire0:~/c[xxx]$ cd u0912345@sunfire0:~[xxx]$ | |
| mkdir | to MaKe a subDIRectory in current directory |
u0912345@sunfire0:~[xxx]$ mkdir another u0912345@sunfire0:~[xxx]$ ls -F another/ c/ doc/ | |
| rmdir | to ReMove a subDIRectory in current directory -- note that a directory must be empty before it can be removed. |
u0912345@sunfire0:~[xxx]$ rmdir another u0912345@sunfire0:~[xxx]$ ls -F c/ doc/ |
| cp | to CoPy files |
u0912345@sunfire0:~[xxx]$ cd doc u0912345@sunfire0:~/doc[xxx]$ cp abridged.txt anotherfile u0912345@sunfire0:~/doc[xxx]$ ls abridged.txt anotherfile faq.txt tutor | |
| mv | to MoVe files from one directory to another; can also be used to rename files. |
u0912345@sunfire0:~/doc[xxx]$ mv anotherfile afile u0912345@sunfire0:~/doc[xxx]$ ls abridged.txt afile faq.txt tutor | |
| rm | to ReMove files. Be careful with this command -- files deleted cannot be restored (unless they have been backed up during the normal backup cycle). |
u0912345@sunfire0:~/doc[xxx]$ rm afile rm: remove `afile'? y u0912345@sunfire0:~/doc[xxx]$ ls abridged.txt faq.txt tutor |
| cat | to string together or display (CATenate) the contents of files onto the screen |
u0912345@sunfire0:~/doc[xxx]$ cat abridged.txt | |
| less | variant of "cat" (includes features to read each page leisurely) |
u0912345@sunfire0:~/doc[xxx]$ less abridged.txt | |
| In "less", use <space> to move down one page, 'b' to move Back up one page, and 'q' to Quit from "less". |
An online help facility is available in UNIX via the "man" command ("man" stands for MANual). To look for more information about any UNIX command, for example, "ls", type "man ls". Type "man man" and refer to this web page to find out more about the facility. To exit "man", press "q".
The process of creating a working C program involves the following steps:
+---------+
+---+-->| EDIT |
| ^ +---------+
| | |
| | |
| | v
| | +---------+
| +---| COMPILE |
| +---------+
| |
| |
| v
| +---------+
+-------| RUN |
+---------+ |
|
| |
|
Three sample C programs have been copied into your "c" subdirectory. Use "cat" or "less" to view the program "ch1_1.c".
u0912345@sunfire0:~/doc[xxx]$ cd u0912345@sunfire0:~[xxx]$ cd c u0912345@sunfire0:~/c[xxx]$ less ch1_1.c
/*---------------------------------------------------*/
/* Program chapter1_1 */
/* */
/* This program computes the */
/* distance between two points. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare and initialize variables. */
double x1=1, y1=5, x2=4, y2=7,
side_1, side_2, distance;
/* Compute sides of a right triangle. */
side_1 = x2 - x1;
side_2 = y2 - y1;
distance = sqrt(side_1*side_1 + side_2*side_2);
/* Print distance. */
printf("The distance between the two points is "
"%5.2f \n",distance);
/* Exit program. */
return 0;
}
/*---------------------------------------------------*/ |
Compile and run the program as follows (use the "gcc" compiler):
u0912345@sunfire0:~/c[xxx]$ gcc -Wall -lm ch1_1.c
(this will compile "ch1_1.c" and produce the executable file "a.out" (Cygwin in Windows generates "a.exe"). Note that the "-lm" flag (dash L M) is used whenever a C program contains the line #include <math.h>. The "-Wall" flag tells the compiler to generate all warnings. Note that the W in "-Wall" must be an uppercase letter. C is case-sensitive.
Note that if gcc compiles your program successfully, you will see no output. With gcc, "no news is good news". You are now ready to execute your program by typing the name of the executable:
u0912345@sunfire0:~/c[xxx]$ ./a.out
This will execute the file "a.out". Note that we prefix it with "./" to indicate that "a.out" is in the current directory. For Cygwin in Windows, you should execute "a.exe" accordingly.
Get into your c subdirectory if you are not there. Enter "vim first.c", then press the "i" key. You will see that the words "-- INSERT --" appear on the bottom left corner of your screen. You are now in INSERT mode. While you are in INSERT mode, you may use the arrow keys (Up, Down, Left, Right) to move around your program, as well as the Backspace key and Delete key to delete text. The PageUp and PageDown keys do not work, so do not use them. Do not use the scroll bar as it does not always work as expected.
Notice that line numbers (1, 2, 3, etc.) are displayed on the left. This helps you to easily identify a number by its line. Line numbers are NOT part of C code. This is extremely useful when identifying the location of compilation errors.
Type in the following program:
#include <stdio.h>
int main(void)
{
int a=27, b=6, c;
c = a%b;
printf("The value of c is %i.\n", c);
return 0;
} |
Note that for simplicity, the above program has no documentation. A good program should include documentation, at least the identity of the author, the purpose of the program and other relevant information. Keep this in mind when you write your own programs.
When you are done, press "<ESC>:x<ENTER>" (more clearly seen as pressing the following four keys one after another: <ESC> : x <ENTER>) to save your program and exit from the Vim editor. <ESC> means press the Escape key, then press the colon key (shift-;), then press x (the x is a lowercase x), and finally <ENTER> means press the Enter key.
If you want to save your file without exiting from the Vim editor, press "<ESC>:w<ENTER>". It is a good habit to save your file periodically so that if the network or the system goes down for any reason, you will not lose your hard work.
When you startup Vim, it begins in COMMAND mode. One way to go into INSERT mode is to press the "i" key. While in INSERT mode, you can type in your C code. To switch back to COMMAND mode, press the "<ESC>" key.
The following shows you a list of useful commands in Vim:
To undo or redo in Vim, ensure that you are in COMMAND mode and press lowercase "u" or control-R respectively. If you would like to learn more about Vim commands, click HERE. Another useful reference can be found HERE.
When you begin writing bigger programs, you will find the need to move around or delete portions of code. To do this in Vim, ensure that you are in COMMAND mode by pressing <ESC>. Using your arrow keys, go to the top (or bottom) of the section of code you want to move, press uppercase "V "to start highlighting. Using your up/down arrow keys, highlight the entire section of code.
Depending on what you want to do to the highlighted sections, you may press "d" for delete, "x" for cut or "y" for copy (or yank). Once a section has been cut or yanked, you may move to the appropriate point in your code and press uppercase "P" to paste.
You may also choose to use your mouse to highlight blocks of text and use the native Window's cut-and-paste functionality, but bear in mind that the end effect may be less desirable. In UNIX, we never use the mouse.
u0912345@sunfire0:~/c[xxx]$ gcc -Wall first.c
There will be error messages if your program has errors. Go to Step 4.2 to make the necessary corrections and re-compile. If there are no compilation errors, proceed with program execution as follows:
u0912345@sunfire0:~/c[xxx]$ ./a.out
Note that this "a.out" (or "a.exe" in Windows Cygwin) file is the executable version of "first.c", and it has replaced the earlier "a.out" file which is the executable file of "ch1_1.c", since there must not be two files with the same name in a directory. To avoid this, you may specify the name of the executable file, instead of leaving it to the system to use the default name "a.out". The trick lies in the "-o" option of the "gcc" command:
u0912345@sunfire0:~/c[xxx]$ gcc -Wall first.c -o first.exe
In the above example, the executable file for "first.c" will be called "first.exe". Note that the name of the executable file MUST IMMEDIATELY follow the -o. Be careful of this or you may lose your source file!
Do NOT ever type the following:
u0912345@sunfire0:~/c[xxx]$ gcc -Wall first.c -o first.cWhat is wrong with the above statement? It states that you want to output the executable file to first.c. This is wrong, because the executable file should NOT be the same as your source program file. If you type the command wrongly, you will LOSE YOUR SOURCE FILE, and you would have to start typing your source file all over again, wasting your time and your effort. So be careful in what you type.
Use the instructions in Steps 4.2 and 4.3 to create, compile and run the following program "second.c". What is the output of the program? How could you add a blank line before and after the line of output, to make it into a 3-line output?
#include <stdio.h>
int main(void)
{
double a;
double b=6, c=18.6;
a = b/c;
printf("b is %3.1f, c is %3.1f. ", b, c);
printf("a is %.6f.\n", a);
return 0;
}
|
Experiment a little by changing the "3.1" and ".6" to some other values and try to figure out what they do. Try also to combine the two printf statements into one. Programming is fun in that you can try out all sorts of things and see the end results yourself! A lot can be learned through self-exploration alone and you needn't worry about setting sunfire on fire!
The program "third.c" below contains syntax errors which will be reported during compilation. Though you may be new to C, the error messages do give some information, and by referring to the few correct sample programs above, you should be able to spot some, if not all, of the errors.
*/ This program contains MANY syntax errors /*
include (stdio.h);
int main(void)
{
double num
num := 305.68;
printf("num is ")
printf('%6.1f\n', Num);
return 0;
}
|
Edit the program edit to correct the errors. Compile it. If there are still errors, what must you do? Yes, edit the program again, and re-compile. Assuming that all errors have been corrected, what is the output of this program?
The following is a walkthrough for your CS1101C lab #0. Before you start, make sure you have the following:
Connection problems
If you have problems connecting to CourseMarker, then most likely you are connecting from outside the NUS SoC network domain. You will need to activate SoC-VPN. You should use your NUSNET account id and password to login to SoC-VPN. Refer to https://noc.comp.nus.edu.sg/docs/using-socvpn for information on how to install and activate SoC-VPN for your specific machine. Note that for Windows Vista users, you have to right click on "Connect to SoC-VPN" and choose "Run as Administrator".
CourseMarker password problems
If you did not receive your CourseMarker password in your NUSNET email account or you suspect your password is wrong, then you can create a new password https://mysoc.nus.edu.sg/~cmarker/get-password.cgi.
If you see the login prompt of CourseMarker above, then you have been successful thus far. The CourseMarker system will prompt you for the following
You have now got access into CourseMarker. Before looking at the different CourseMarker commands, let's have a high level view of the system. The CourseMarker system is organized as a hierarchy of the following levels:
As shown in the preceding screen shot, a successful login to Coursemarker starts you off at the system level. At each level, the valid commands are listed in the [] brackets. For example, at the system level, the valid commands are "lc" and "vm". We only cover those commands that are useful for your lab submission; you can ignore those not mentioned in this guide.
Enter "lc" (short for List Courses), which shows all the courses you have under CourseMarker system. In our case, there will be only one course "cs1101c : no title". To navigate to the course level, enter "cs1101c". You will be presented with another set of commands [ vm csum lu up ]. The "up" command moves you back one level (back to the system level in this case). The "lu" command lists the available units (labs).
Enter "lu" and you should see one unit listed as "lab00 : no title". Enter the unit name "lab00" to get to the unit level. At unit level, there are four commands [ lx vn usum up ]. "up" moves you back to the course level and "lx" lists the available exercises for this lab.
Enter "lx" and you should see "ex1 : Cube". Enter "ex1" to reach the exercise level.
The exercise level is the most important level, so spend some time understanding the various commands. Initially, you will only see three commands: [ set vq up ]. "set" downloads the relevant files (question, skeleton program files, etc) from the system to your directory. "vq" (view question) displays the lab problem, and "up" transports you back to the unit level.
Enter "set" to download the necessary files. If the command is successful, you will see quite a few more commands at the prompt.
The additional commands do not concern us at the moment. Enter "quit" to exit CourseMarker and return to the UNIX prompt.
The "set" command overwrites any existing file in the exercise directory. Use this command once per exercise. If you use the "set" command after having written some code, your work may be overwritten!
The various files downloaded from CourseMarker are stored under the following directory:
~/CMhome/cs1101c/studentArea/yourCourseMarkerId/cs1101clab00ex1
where yourCourseMarkerId refers to your personal CourseMarker ID. The last directory "cs1101clab00ex1" is simply the course name, unit name and exercise name joined together. Navigate to this directory using the "cd" command. If you are not sure how to do it, here's one way:
u0912345@sunfire0:~/c[xxx]$ cd ~/CMhome/cs1101c/studentArea/yourCourseMarkerId/cs1101clab00ex1
Enter "ls" to view the downloaded files. Note the following files:
Before you start coding, make sure you read the specifications on the input and output carefully. Proceed to code the program in the given skeleton program file "Cube.c" using Vim. Develop and debug your program until it can be compiled successfully to produce an executable (see section 4.3). Note that the executable should be the name of the source code followed by the extension ".exe" (in this case, the executable is "Cube.exe"). The correct executable is necessary to proceed with submission to CourseMarker. Run your program and ensure that it is in good working order before submitting.
To submit, start up CourseMarker by issuing the command "cs1101cCM" again. Note that the CourseMarker system automatically resumes from where you left off. If you followed closely to the steps given above, you should find yourself in the "ex1" exercise level as shown below.
If you are not at this level, just use the appropriate commands to navigate to the correct level. You will see more commands available now, in particular, the "submit" command. This command is enabled automatically as soon as CourseMarker detects the executable for the particular exercise. If this command is missing from the choices, then the most likely reason is that you failed to compile the program.
Type "submit" to submit your work. If the submission is successful (may take some time depending on the system load, so please be patient), CourseMarker will give you an assessment of the submission. The result is too long to fit into one screen, so use the Window scroll bar to view the full report. The full report looks something like this (only partially shown):
(L1)=========================== Mark:100.0 Grading:A Cweight:100.0 Description:General Grade Feedback:Awesomely Excellent ============================== (L2)=========================== Mark:100.0 Grading:A Cweight:0.0 Description:Compilation Tool Feedback:Awesomely Excellent ============================== (L3)=========================== Mark:100.0 Grading:A Cweight:100.0 Description:Search for errors in the compilation Feedback:No errors found during complilation ============================== //... ... Snipped ... ... ... ... ... ... ... // (L2)=========================== Mark:0.0 Grading:F Cweight:0.0 Description:Feature Tool Feedback:Not so good ==============================
At first glance the report is quite confusing. However, you will notice the labels (L1), (L2), etc, organized as a hierarchy. The hierarchy looks like this (only description of each level is shown):
(L1)===========================
Description:General Grade
==============================
(L2)===========================
Description:Compilation Tool
==============================
(L3)===========================
Description:Search for errors in the compilation
==============================
(L3)===========================
Description:Search for warnings in the compilation
==============================
(L2)===========================
Description:Typographic Tool
==============================
(L3)===========================
Description:Avg. chars per line
==============================
(L3)===========================
Description:Avg. spaces per line
==============================
(L3)===========================
Description:% of blank lines
==============================
(L2)===========================
Description:ALL Dynamic Tests
==============================
(L3)===========================
Description:Test 1 -
==============================
(L4)===========================
Description:Test #1a
==============================
(L3)===========================
Description:Test 2 -
==============================
(L4)===========================
Description:Test #2a
==============================
// ... Similarly for Test 3 and 4 ... ... //
(L2)===========================
Description:Feature Tool
==============================
As you can see, the report is organized into levels (or sections). The topmost level General Grade gives you the summary score for the whole lab. The sub-sections below give the detailed breakdown of the marking. You should concentrate on only two subsections: Compilation Tool and All Dynamic Tests.
Note that test cases include those that were provided to you, as well as unseen ones. Quit CourseMarker and go back to the your working directory that contains your source program file for a particular exercise. You would have noticed the files "test1.in", "test1.out", etc. in the exercise directory. These files contain the input and output used by the CourseMarker for dynamic tests. Files with the extension ".in" are the test inputs; while files with ".out" extensions are the corresponding expected output. Test cases are also numbered.
You can view the content of these files using "less" or "cat". For the first test case, the contents of "test1.in" is "20 10 5"; the contents of "test1.out" is "Volume = 1000". Run the program executable and enter "20 10 5" (same as "test1.in") as input. If your output is exactly "Volume = 1000" (same as "test1.out"), then your program is correct for this particular test case.
Other than manual input (which can sometimes come up to tens or hundreds of lines), the program can be tricked into reading a file as if it is reading from user input. You can do this by running the program using
u0912345@sunfire0:~/.../[xxx]$ ./Cube.exe < test1.in
You should then see the same output "Volume = 1000". This trick is known as redirection in UNIX. Try using the redirection with other test input files. Redirection also works for output, i.e. you can capture all the output into a file. Run the executable using
u0912345@sunfire0:~/.../[xxx]$ ./Cube.exe < test1.in > output1.out
The above says "execute Cube.exe, read input from test1.in, and place all output in output1.out". Pay attention to the direction of the arrow < and >. You should find that the program ends without showing any output. This is expected as all input have been read from test1.in, and all output written to output1.out. To verify, enter "less output1.out" to see the output produced by your program.
Make sure you use a new output file name and not "test1.out". Be careful of the output file name you use for redirecting output. Redirection may destroy the original content of the file! If you find that running the command repeatedly results in UNIX forbidding you to redirect output to an existing file, you can use >| instead of > to force the redirection.
Since output has now been redirected to the file "output1.out", the "diff" command can be used to compare it against the expected output by entering
u0912345@sunfire0:~/.../[xxx]$ diff output1.out test1.outThe "diff" command compares two files and reports any inconsistencies. If you see nothing on the screen (remember no news is good news), then you are assured that your program works perfectly for that one test case. Any discrepancies between the two files, will result in the offending lines shown on the screen. Suppose your program produced the result "volume = 200" instead of "Volume = 1000". You will see something of the following from "diff".
1c1 < volume = 200 - - - > Volume = 1000The "diff" command shows you the corresponding lines from the two files so that the difference can be clearly seen. Lines that start with < refer to the leftmost file (in this case "output1.out"), while lines start with > refer to the rightmost file (i.e. "test1.out"). Use this information to figure out the possible error in your logic and correct them, then re-edit, re-compile and re-run all tests to make sure there is no difference in the output. Do the same for the other test cases. In particular, check the assessment report and concentrate on those test cases that failed the dynamic tests.
A summary for program development in CourseMarker is given below:
For lab #0, you must get at least 3 test cases correct to get the 1% allocated for this exercise. Please be reminded that for all subsequent labs, the percentage of test cases you pass is the percentage you can get from the 70% of the correctness score.
These are the files copied into your home directories by the setup program in sunfire, or preloaded in the cygwin/home folder.
In your "c" subdirectory:
In your "doc" subdirectory:
For example to copy the file "ch1_1.c" from the local "c" directory to the home directory in sunfire, enter the command from the local UNIX prompt.
localuser@localmachine:~[xxx]$ scp ~/c/ch1_1.c u0912345@sunfire-r.comp.nus.edu.sg:~Note that the source is "~/c/ch1_1.c" and the destination is "u0912345@sunfire-r.comp.nus.edu.sg:~".
To copy a file "ch1_2.c" from the "c" directory in sunfire to the home directory with a different file name "prog.c" on your local machine, enter the following command from the local UNIX prompt.
localuser@localmachine:~[xxx]$ scp u0912345@sunfire-r.comp.nus.edu.sg:~/c/ch1_2.c ~/prog.cNote that the source is "u0912345@sunfire-r.comp.nus.edu.sg:~/c/ch1_2.c" and the destination is "~/prog.c".
You can navigate around both panels to copy/delete files. To transfer a file from sunfire to your local machine, right-click on the file you wish to transfer, and then choose "Download". To transfer a file from your local machine to sunfire, right-click on the file you wish to transfer, and select "Upload". Be sure to change to the correct folder/directory at the destination before performing the transfer.