Intro to C #1: select-column
Starter code: select-column-package.tar
Upload solutions via Moodle as: select-column.tar
Goals
- Get started with some of the fundamentals of C programming
- Get used to using some simple program testing automation in the form of a Makefile and sample test data
- Get experience reading man pages
Rubric
Getting started with C
This term, nearly all of your work will involve the C programming language either directly or indirectly. As we get started with C, I am assuming that you have a working knowledge of both Python and Java, to the level they are taught in CS 111 and 201.
I also assume you will familiarize yourself with the program examples I provide on our Samples page. You should definitely study and play with those programs, and you may feel free to plunder them for code to use in your own programs.
Your first few homework assignments will focus on a few core C concepts:
- program structure:
main
,#include
, functions, and function prototypes - input:
scanf
,fscanf
,fgetc
, andfgets
- output:
printf
,fprintf
,fputc
, andfputs
- essential variable types:
char
,int
,float
,long
, anddouble
- conditionals:
if
,else
, and boolean expressions - loops:
while
andfor
- arrays
- null-terminated
char
strings along with their associated library functionsstrlen
,strcmp
,strncpy
, andstrncat
- command-line arguments with
argc
andargv
- structs
- pointers, including the operators
*
,&
, and->
- memory allocation:
malloc
andfree
CSV files
The input for your first program will come in the form of a comma-separated values file. CSV files give us a simple way of representing tables. Each line of text represents one row of the table, and each row is separated into columns by commas. For example, here is a simple CSV file animals.csv representing a table with a heading row followed by five data rows, with three data cells (or columns, if you prefer) per row:
The official CSV format has to worry about whether any of the data in the cells contain commas, but we're not going to worry about that. For this assignment, you may assume that there are no commas in the data, so any comma in the CSV file marks a separation between adjacent data cells.
Programming assignment #1: select-column
For this assignment, you will write a C program select-column.c
that takes a CSV file and a 1-based column number as command-line arguments, and
prints the specified column of the file to standard output.
For example, suppose we run your program like so:
where the file animals.csv is as shown above. Then the expected output is:
If the specified column does not exist in a particular row of the file, print an empty line for that row.
Getting the starter package
For many assignments this term, you'll receive some starter code, some testing tools, or miscellaneous other materials to help you get started. These will generally be delivered to you via downloadable tar files. As noted in this handy tutorial from Indiana University, you can extract the files and folders contained in a tar file by using the command:
To get started on this first assignment:
- Login to mantis.mathcs.carleton.edu using VS Code and open your cs208 folder
In your VS Code terminal, run:
wget https://cs.carleton.edu/faculty/jondich/courses/cs208_w24/assignments/packages/select-column-package.tarStill in your VS Code terminal, extract the select-column-package folder:
tar xvf select-column-package.tarThis will create a folder named "select-column-package" with some stuff in it.
- Read the readme.txt file and get started.
Automated testing
In the select-column-package.tar file linked at the top of this page, you will find:
- Makefile: a file that you'll use to compile your program and run one very simple test
- readme.txt: an explanation of how to use the Makefile
- some test data files
For most assignments, I will only give you very simple tests in the assignment's starter package. The grader and I will run some more sophisticated tests to explore the boundaries of a given assignment. You are, of course, free to use the testing infrastructure from the starter package to add your own tests. Getting used to automated testing and to writing detailed tests of your own will serve you well in the long-run.
Submitting your work
- put your source file select-column.c in a folder named select-column
- cd to the parent directory of select-column/
create a tar file:
tar cvf select-column.tar select-column- download the tar file to your local machine (in Visual Studio Code while connected to mantis.mathcs.carleton.edu, you can right-click on starting-c.tar and select Download)
- use the Moodle web interface to submit your tar file
A little advice
- Think ahead of time about error handling. What could go wrong with the command-line or the input file or anything else in your program? What should your program do when those things happen?
- Think ahead of time about testing. What special cases should you test? How can you test them?
- Assume that each line ends either with a newline character
\n
(ASCII 10) or with the end of the file. You'll want your program to work even if the last line doesn't end with\n
. - For this program, you won't need to read entire lines into memory. This problem
is solvable just reading one character at a time using
fgetc
.
Have fun!
Don't by shy about experimenting and asking lots of questions.