CSC230 Homework 1
- There were a few instances of ">!" below; this is syntax for the tcsh shell, which is no longer used at NCSU. We now use the bash shell, where the correct syntax is simply ">".
The purpose of this assignment is to get you started writing, formatting, compiling, and executing simple C programs. This requires that you familiarize yourself with the Common Platform (NCSU Linux machines) and learn how to use gcc for compiling C source code to an executable.
This homework is to be done individually.
Learning Outcomes
- Write small to medium C programs having several separately-compiled modules.
- Explain what happens to a program during preprocessing, lexical analysis, parsing, code generation, code optimization, linking, and execution, and identify errors that occur during each phase. In particular, they will be able to describe the differences in this process between C and Java.
- Correctly identify error messages and warnings from the preprocessor, compiler, and linker, and avoid them.
- Find and eliminate runtime errors using a combination of logic, language understanding, trace printout, and gdb or a similar command-line debugger.
- Interpret and explain data types, conversions between data types, and the possibility of overflow and underflow.
- Explain, inspect, and implement programs using structures such as enumerated types, unions, and constants and arithmetic, logical, relational, assignment, and bitwise operators.
- Trace and reason about variables and their scope in a single function, across multiple functions, and across multiple modules.
- Allocate and deallocate memory in C programs while avoiding memory leaks and dangling pointers. In particular, they will be able to implement dynamic arrays and singly-linked lists using allocated memory.
- Use the C preprocessor to control tracing of programs, compilation for different systems, and write simple macros.
- Write, debug, and modify programs using library utilities, including, but not limited to assert, the math library, the string library, random number generation, variable number of parameters, standard I/O, and file I/O.
- Use simple command-line tools to design, document, debug, and maintain their programs.
- Use an automatic packaging tool, such as make or ant, to distribute and maintain software that has multiple compilation units.
- Use a version control tools, such as subversion (svn) or Git, to track changes and do parallel development of software.
- Distinguish key elements of the syntax (what’s legal), semantics (what does it do), and pragmatics (how is it used) of a programming language.
Part 1: Hello World
Download hw1_starter.tar. A tar file is a container for a group of files. We have grouped together all of the files needed to get started for the homework. FTP the tar file to your AFS space and untar using the following command:
Alternatively, you can download the files individually and FTP them to your AFS space. Be careful when FTPing the expected output files. There may be differences with white space that will lead to deductions!
Open the "Hello World" program in your favorite text editor. It has been commented and formatted according to the style guidelines for the class.
The purpose of the Hello World program is to demonstrate your ability to
compile a program on the common platform and to refresh your memory for
writing simple functions. You will add two functions to the Hello World
program (functions are basically C's version of Java methods). These functions
would be written in C almost the same way they are written in Java, so if
you struggle with this assignment, it's an indication you may need to brush
up on your 116 and 216 knowledge. The descriptions of the functions are
in the hello_world.c
file.
Complete the following tasks:
- Modify the top level comments as appropriate (use your name, the actual date, etc.).
- Modify the program to output
"Hello World!\n"
in the first print statement ofmain()
. - Compile with options
-Wall -std=c99
. - Test your program to make sure it executes correctly.
- Complete the
fizzbuzz
andpyramid
functions. - Compile and test your program again.
- Submit your modified source code
Use the following command to compile your program:
Use the following commands to test your program by generating an output file using redirection and then comparing your actual output of execution with our expected output from execution:
If there is no output (the two files are the same) then you successfully completed the assignment!
The program you submit should be named hello_world.c
.
Part 2: Mystery
Reformat and rename mystery_unformatted.c in accordance with the class style guidelines so that it is clear, consistent, and easy to read. You may use any tool you like for this purpose, including an indenting tool, an IDE, a text editor, etc.
When your program runs, the output should be exactly:
After reformatting the program, read the code and figure out what the program is doing (at a high level). Please add a comment to the main function in accordance with the style guidelines that explains the functionality of the program in one or two sentences.
When grading, we redirect your program's output to a file. We will then diff
your output with our output. There should be NO difference (if there is a difference, the information about the difference is printed to the console). This would be done using the following commands (assuming the expected output is stored in mystery_expected):
You can also use the sdiff
command. This will print both files side by side and allow you to find the exact inconsistencies between the files. In the command above, you would replace diff
with sdiff
.
The program you submit should be named mystery.c
.
Instructions for Submission
This github isn't ready yet, so we'll just use submit.ncsu.edu for now. How to:- Go to submit.ncsu.edu.
- Hit "Submit an Assignment"
- Select "CSC230 - 051 (SumI)"
- Choose the "HW1" locker.
- Upload a file called <unityid>_1_homework.tar containing your hello_world.c and your mystery.c. The files must have these exact names. Replace [unityid] with your Unity ID.
We recommend that you keep the files for Homework 1 in a separate directory from your other course materials. Assuming that this is so, use the following command (in the directory with your Homework 1 files) to tar
your files:
This command will tar all of the *.c
files in the current directory to a file named <unityid>_1_homework.tar
.
Again, please replace <unityid>
with your unity id.
As the tar is created, all the files added to the tar will be listed so you can confirm that the appropriate files are submitted.
Follow the Style Guidelines for CSC230.
Make sure your program compiles on the common platform cleanly (no errors or warnings), with the required compiler options.
Rubric
Part 1: hello_world.c
+10 for compiling on common platform with
gcc -Wall -std=c99
options, with no warnings+20 for correct results (as per the
diff
command shown above)+20 for changing the name in the comments and maintaining the style guidelines for the course
(!!!) -5 for not including your name
-5 for formatting that is inconsistent, makes no sense, or is hard to read (e.g., no tabs)
Part 2: mystery.c
+20 for changing the formatting to match the style guidelines for the course
-5 for each formatting mistake (e.g., no tabs), up to 20 points
+20 for correctly describing the program functionality.
+10 for correct results (as per the
diff
command shown above)
Commons sources of Error
- After putting your program into a tar file and downloading it, be sure that the required files are in the tar file before submitting. You can use a tool such as 7zip or Winrar to open these files on windows (Macs and Linux machines should be able to open them normally). Many students lose points for submitting empty tar files or tar files with missing contents.
- If a program enters an infinite loop, use Ctrl+C to stop it.
- Never hesitate to contact the course staff when encountering technical difficulties. Always contact the sup list for your section, not just your instructor.
- We do not accept submissions via e-mail.