Preparing for CSE 340 programming projects

Download Preparing for CSE 340 programming projects

Preview text

Preparing for CSE 340 programming projects
Mohsen Zohrevandi
Last Update: August 21, 2015

We have an automated grading system for this course that evaluates your programs by running them on multiple test cases and comparing the output with the expected output for each case. This requires a single development platform to be used by all students and the grading software. In this semester we have chosen to work on CentOS 6.7 because the 2nd floor lab has been equipped with machines running this flavor of Linux.1 This document explains how to write code compatible with the automated grading system. It also provides instructions on how to install CentOS on a virtual machine so that you can develop and test programs on your own machine.


1 Project Grading


1.1 Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.2 Standard I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3 Test Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Development Tools


2.1 Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.2 Compiling Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.4 Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Installing CentOS on a virtual machine


3.1 Creating a virtual machine in VirtualBox . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.2 Installing CentOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.3 Installing Guest Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.4 Sharing files with the VM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1 The lab has been equipped with Red Hat Enterprise Linux which is binary compatible with CentOS. See the following page for more information:

1 Project Grading
We grade the programming projects with a shell script that automatically compiles and runs your programs on a set of test cases.
1.1 Test Cases
All assignments have a precise input/output specification that allows one to test the correctness of the program by comparing the program’s output with the expected output. For each project we provide a description of the program’s desired functionality along with input/output format and a set of sample test cases. Each test case consists of an input file and an expected output file. The grading script runs your program for each test case. It runs the program with the input file as input and compares the output generated by your program with the expected output. Each test case is either passed or failed depending on the result of the comparison. The files are compared with the following command:

If there are any differences between the two files, format, otherwise it will not output anything.2

will output the differences in a special

Usually the set of test cases used for grading contains some additional test cases that are not available to students. The additional test cases might cover some special cases that are not represented in the sample set, so it is very important that you read and fully understand the project specification and not just rely on sample test cases. Your project grade will be a function of the number of test cases that your program correctly passes.

1.2 Standard I/O Redirection
Your programs should not access any files. In other words, you should not open any file in your program for reading or writing. Instead, your program should read the input from standard input and write the output to the standard output. Also, any output to standard error is ignored by the test scripts so you may use standard error for debugging messages. To feed the contents of a text file as input to a program, we use I/O redirection. Suppose we have an executable program
, we can run it by issuing the following command in a terminal (the dollar sign is not part of the command):

The above command runs the program and if the program uses standard I/O, it will print

messages on the terminal screen and wait for input from keyboard. Now suppose we have a text


that contains all the input for our program. We can redirect the standard

input to our input file:

2 If you are interested to learn more about

and it’s options, you can find more information in the following page:


The followed by a file name is used to redirect standard input to a file. This way the program

will not wait for input from keyboard, instead, it will treat the contents of

as if it

were entered on the keyboard. The output is still printed to the terminal screen. To redirect the

output to file

, we issue the following command:

The followed by a file name is used to redirect the standard output to a file. Note that the above command does not redirect standard error, and if the program prints anything to standard error, it will appear on the terminal screen while the normal output is written to the specified file.

To put everything in an example, let’s assume that we need to write a program that reads any

alphanumeric, space and newline characters from the input and prints or if it encounters any

other character, it prints

to the standard output. Here is a C program that does the job:

Now we compile the program:

The executable file is

, if we execute it normally:

The program waits for input from keyboard and you can press

to simulate end of file. It


or on the terminal screen. Now let’s create a test case for it. Here is the contents

of a text file named


We expect the program to generate an OK for this input:


We can automate this by creating a text file named

with the following contents:

Then to verify the program, we redirect both input and output, and then compare the output to the expected file:

If the program produces output according to the expected file, we should see no output from the

command. Otherwise

will show the difference between the output generated by the

program and the expected output.

1.3 Test Script
As mentioned before, the grading and testing are automated using shell scripts. In this section I give you details of the test script that you should use to test your programs during development. The test script is a Bash script and can be executed by:

The script file needs execution permission otherwise it won’t run and you will get the following error message:

To fix the problem, issue the following command:

The test script assumes that there is a sub-folder named

containing all the test cases. It also

assumes that your executable program is named

and is in the current folder. It runs your

program with I/O redirection for each test case in the tests folder and compares the output with

the expected file using

and reports any differences for failed cases. At the end it will print

the number of tests passed and removes any temporary files created. Here is the script source



The last two lines of the script remove the output files of your program and the results of . If you want to examine the output of your program, you can comment out the last two lines of the script by putting a at the beginning of the lines:

2 Development Tools

2.1 Platform

We will use the GCC compilers that come standard with CentOS 6.7 to compile your programs. To avoid any compiler compatibility issues, please use this version of GCC3 for program development.
Here is a list of most important things to keep in mind:

• Use CentOS 6.7 and its standard GCC compiler for all CSE 340 projects.

• The grading is automated, so stick to the input/output specification of the project.

• Only use standard input/output. Do NOT open any files in your code.

• The test cases are fed to your program with standard I/O redirection. See section 1.2 for details.

• Use the test script that will be provided later to test your program for all projects. The test

script uses the same technique as the grading scripts. It tells you how many test cases are

passed and gives you the result of

for failed cases so that you can fix the problems.

3 Currently version 4.4.7. Note that there are newer versions of GCC that might have additional features not present in this version of the compiler. Avoid using other versions of the GCC compiler.


2.2 Compiling Programs

You should compile your programs with the GCC compiler which is available in CentOS 6.7. The

GCC compiler has separate commands for compiling C and C++ programs, use to compile

C programs and use to compile C++ programs. Here is a simple command to compile a C

program that is stored in file


If the compilation is successful, GCC will generate an executable file named folder as the source file. You can change the output file name by switch:

in the same

To enable all warning messages of the GCC compiler, use



switch will report all sorts of useful information about your program like unused

variables, etc. The same options can be used with to compile C++ programs.

If your program is written in multiple source files that should be linked together, you can compile and link all files together with one command:

Or you can compile them separately and then link:

The files with extension are object files but are not executable. They are linked together with

the last statement and the final executable will be

if there are no errors. To automate the

compilation process you can write a Makefile, see section 2.4 for details.

2.3 Debugging
There are debugging tools available for CentOS that can be used to trace your program execution, see GDB and DDD for example:

GDB is very powerful, but it does not have a nice user interface like other debuggers you might be familiar with. DDD provides a GUI frontend for GDB. The DDD installation is not straightforward, but you can find instructions in the second URL above in the Install Howto section. Remember that

to install packages you need to be in root mode, so switch to root by issuing

the root mode by

command after you finish installing packages.

command and exit

You can also debug your programs by printing meaningful messages at certain points in your

code. As mentioned in the previous section, all output to

is ignored by the test scripts, so

you may print your debugging messages to standard error. Here is a small C program that reads a

character from standard input, then writes a message to standard output and another message to

standard error:

However, standard error is not buffered while standard output is buffered, so using both might

result in some confusion in the order of messages printed. Alternatively, you can avoid using

for debugging and use

with an extra flag to turn on/off debugging messages, here

is an example:


A more complex

function that accepts a format string and variable number of

parameters and can be used like

is listed below:

In C++, you can use to read from standard input, to write to standard error.

to write to standard output and

2.4 Makefiles

Makefiles are used to automate the compilation process. You can store all compiling instructions

in a makefile and then just type

to compile the program. Our grading scripts compile your

programs automatically by detecting the language (C or C++) based on file name extensions and

running gcc or g++ with no additional options. However you may need to pass certain compiler

options to gcc in order for your program to be compiled correctly in which case you can provide a

Makefile with your project that tells us how to compile your program.

The compilation script automatically searches for a makefile in the files you submit. The script

expects the name of your makefile, if you have one, to be

(note the capital M in the

name and the absence of an extension in the file name). No other file names are accepted.

The reference manual for

can be found at:


If you need to write a Makefile for your project, you can use the following generic Makefile. Just remember to adjust it for your needs: Do not copy/paste the Makefile from this pdf, download the Makefile from the course website.

3 Installing CentOS on a virtual machine
If you prefer to work on your own machine rather than going to the lab, you can install CentOS 6.7 on your machine. Since it is highly unlikely that you would want to switch to CentOS for all your activities, it is better to keep your own operating system intact and install CentOS on a virtual machine. A virtual machine management (VMM) software allows you to create virtual machines and install all kinds of operating systems on them. There are many different VMM softwares available, some free of charge and some not. VirtualBox is one example that is free and open source. It can be installed on different platforms including Microsoft Windows and Mac OS X. You can use the following URL to download VirtualBox for your machine:
In VMM parlance, the operating system that you already have on your machine is called the host OS and the operating system that you install on a virtual machine is called the guest OS. The following page contains an installation guide for different host operating systems:

3.1 Creating a virtual machine in VirtualBox
Follow the instructions in the VirtualBox user manual listed below to create a new virtual machine:

Use the following settings when creating the virtual machine:

Name: Type: Version: Memory size: Hard drive type: Storage method: Disk size:

centos Linux Red Hat or Red Hat (64 bit) depending on your machine’s architecture 1024 MB VDI Dynamically allocated 8 GB

After finishing the wizard, you will have a new entry in the VirtualBox window for the VM you created. Now you can start the VM and install CentOS 6.7 on it, but before that you need to download an ISO image of CentOS 6.7. Don’t start the VM yet, follow the instructions in the next section to download and install CentOS on your VM.


Preparing to load PDF file. please wait...

0 of 0
Preparing for CSE 340 programming projects