CSPP 50101 - Introduction to Programming


6/28 - As a part of my patriotic duty, I have rescheduled the July 5th class to Wednesday, July 7th. Class will still start at 5:30PM in Ryerson 251 (room is still unconfirmed.)

7/19 - Note that assignment 4's due date has been moved to next week (7/26), and if you turned in something today, that homework will receive a 20 point bonus.

Vital Information

Before saying anything further, I'd like to note that this is the website for the Summer 2004 incarnation of CSPP 50101, and is not to be confused with prior courses.

Meeting time and place: Mondays, 5:30PM - 8:30PM, Ryerson 251.

Instructor: Jonathan Riehl
Office: Hinds B-024A
Phone: Maybe one day the department will blow their budget and get me a tin can and string...
Email: jriehl@cs.uchicago.edu

Teaching assistant: Zhimin (Jim) Xie
Office hours: Thursdays from 5:00 PM to 7:00 PM in the 4th floor lab in Ryerson.
Email: zxie@cs.uchicago.edu


There is one required text:

Stephen Prata, C Primer Plus (4th ed.) IBSN 0-672-32222-6.

I also recommend just about anything on Andrew Siegel's page. Of course, the definitive text on C programming is:

Brian Kernighan and Dennis Ritchie, The C Programming Language (2nd ed.) IBSN 0-131-10362-8.


Grades will be determined using a careful mixture of in class quizzes, homework assignments, and a final examination:

20% (10%/ea)Quizzes
30%Final examination

Programming Assignments

Homework will be more or less weekly, for a total of about 10 assignments. Unless otherwise noted, homework will be due by the start of class that follows the class the homework was assigned. Late homework will be graded at a penalty of 10% a day.


Part 1: Get an account on the Linux cluster.

Part 2: Send me email with an address you want to be subscribed to the class mailing list.

Part 3.1: Prata, Ch.2, Exercise 3: Write a program that converts your age in years to days and displays both values. Don't worry about leap years or fractional years. You are more than welcome to make up an age if you are self conscious about your age.

Part 3.2: Prata, Ch.2, Exercise 7: Write a program that calls a function one_three(). This function should display the word "one" on one line, call another function called two(), and display the word "three" on one line. The two() function should in turn display the word "two" on a single line. The main() function should display an opening line saying "Starting now:", call one_three(), and then output a closing line saying "All done." The output should look like:

Starting now:
All done.

If you have the book, you may notice I embellished the output text a little. Your program may output either text.

6/28Prata: Chapter 3, exercises 2 and 6.
7/5Prata: Chapter 4, exercises 2 and 7. Chapter 5, exercise 1.

Program 1: Prata, Chapter 6, exercise 4.

Program 2:

Part 1: Write a function that has the following interface:

double average (int size, double values []);

The function should return the average of the first size values in the array values.

Part 2: Write a function that has the following interface:

double promptForData (int index);

The function should read a double precision floating point number from the console after printing: "Enter item %d : ". Where %d is the index number.

Part 3: Write a program that asks for a number of numbers to be input. Read up to that number, but no more than 50, double precision floating point numbers into an array. After each number is input, print a running average, calculated with the average() function described above. If the average is the last average to be displayed, indicate it is the final average.


Enter number of values to be input : 5
Enter item 1 : 22.3
Current average is 22.3.
Enter item 2 : 54.2
Current average is 38.25.
Enter item 3 : 74 
Current average is 50.1667.
Enter item 4 : 83.1
Current average is 58.4.
Enter item 5 : -90. 
Final average is 28.72.

Number of values : 500
Too many values, only reading first 50.
Enter item 1 : 0.9
Current average is 0.9.
Enter item 2 : 1.8
Current average is 1.35.
Enter item 50: 92.3
Final average is 50.0.

Enter number of values to be input : -234
Final average is 0.

Include in your printout some example output of both programs.


Read: "man putchar", "man getchar", and Prata, chapter 8.

Code: Prata, Ch. 7, Ex. 10. The loop should also report the current average taxable income and the number of filers in each category. See ch7ex8.c.


Part 1: Do chapter 9, exercises 7 and 8 as one program. Name the iterative function power(), and the recursive function either r_power() or rPower(). Show the results of both functions side by side.

Part 2 (BONUS 50%): Redo assignment 5's coding assignment using the following main function (you may reformat where necessary):

int main (void)
  char incomeCategory;
  int totalFilers = 0;
  int filerCounts [4];
  double income = 0.0;
  double tax = 0.0;
  double totalIncomes = 0.0;

  incomeCategory = doMenu();
  while (incomeCategory != 'q')
    income = getIncome();
    tax = calculateTax(incomeCategory, income);
    updateStats(incomeCategory, income, &totalFilers, filerCounts,
    doReport(incomeCategory, income, tax, totalFilers, filerCounts,
    incomeCategory = doMenu();
  return 0;

If you chose not to do this, read this code carefully and be sure you could write function prototypes for the functions called.


Reading assignment: Prata, Chapter 11.

Programming assignment (part 1 of 2, Both parts will be due till Monday, August 16th, and count for two homeworks total):

Part 1.1: Write a function with the following prototype:

void clearBoard (int rows, int columns, int * boardPtr);

The function should write a zero value to each element in the passed 2-D array.

Part 1.2: Write a function with the following prototype:

void showBoard (int rows, int columns, int * boardPtr);

The function should display the array. If the value of a board location is 0 to 9, print that value, otherwise print "x". Example:

int board [3][3] = { {0, 0, 1}, {99, -2, 7}, {-1, 6, 0} };
/* ... */
showBoard(3, 3, board);
/* ... */

Should display:

0 0 1
x x 7
x 6 0

Part 1.3: Write a function that reads board coordinates, with the following prototype:

int getCoords (int maxRow, int maxCol, int * row, int * col);

The function should read a character followed by a number, and convert these into a row, column pair that is output through the row, col parameters. The function should return 0 if an invalid entry was made, or a 1 if a valid entry was made. Example:

printf("Enter coordinates : ");
while (0 == getCoords(3, 3, &myRow, &myCol))
   printf("Bad coordinates!\nEnter coordinates : ");
printf("myRow = %d, myCol = %d\n", myRow, myCol);

Example output:

Enter coordinates: yy zz
Bad coordinates!
Enter coordinates : 0 0
Bad coordinates!
Enter coordinates : A10
Bad coordinates!
Enter coordinates : A 9
Bad coordinates!
Enter coordinates : B 2
myRow = 1, myCol = 1

The following chart might help illustrate how this coordinate system works:

    1     2     3     4     ...
A (0,0) (0,1) (0,2) (0,3) ...
B (1,0) (1,1) (1,2) (1,3) ...
C (2,0) (2,1) (2,2) (2,3) ...
D (3,0) (3,1) (3,2) (3,3) ...

Where the output row and column numbers are given as (row, column).

Part 1.4: Write a function with the following prototype:

void paintSpace (int rows, int cols, int * boardPtr);

The function should read a "Battleship" coordinate using the getCoords() and an integer value from the console. The integer value should be written to the board array corresponding to the input coordinate.

Part 1.5: Write a menu loop (in main(), for now) that asks the following:

0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
Your choice? _

If they select 0, zero all the values of the board. If they select 1, ask for coordinates and an integer value to write to the board. Then write that value to the board. If they select 2, display the board using the showBoard() function. If they select 3, tell the user that the game isn't ready yet. If they select 4, exit the program. Otherwise, tell the user their input was invalid. Use a 10x10 board.


This programming assignment (part 2 of 2) is due (along with part 1) via electronic submission. Attach your code to an email that has in the subject line: "CSPP 50101 - Assignments 7 and 8". Send the email to zxie@cs.uchicago.edu by 11:59 P.M. on Tuesday, August 17, 2004.

Part 2.1: Write a function with the following prototype:

int countPieces (int rows, int columns, int * boardPtr, int counts [9]);

The function should build a histogram (counts of values, indexed by value) of the "pieces" on the board, where a "piece" is represented by a value between 1 and 9. Therefore if there are two 4's on the board, then counts[3] == 2 when the function is finished. Be sure to initialize the counts array to zero counts at the beginning. The countPieces() function should return the total number of pieces it counted. Zero (0) values or invalid values (anything other than 0 to 9) should not be counted.

Part 2.2.: Write a function with the following prototype:

int fire (int row, int column, int rows, int columns, int * boardPtr,
          int * firePtr);

This function will accept a (row, column) input. If a "piece" (a value between 1 and 9) is detected at that coordinate on the board (input as boardPtr), then the function should (1) set that space on the board to a 0 value, (2) set that space on the firePtr input to a -1 value, and (3) return the old value found on the board. If the space is blank, the function should (1) set that space on the firePtr board to a 1 value, and (2) return 0. For example:

int boardPtr [3][3] = {{0, 1, 0}, {0, 0, 0}, {0, 2, 2}};
int fireData [3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};

if (fire(0, 1, 3, 3, boardPtr, fireData) != 0) {
} else {
showBoard(3, 3, boardPtr);
showBoard(3, 3, fireData);
if (fire(1, 1, 3, 3, boardPtr, fireData) != 0) {
} else {
showBoard(3, 3, boardPtr);
showBoard(3, 3, fireData);

Will output:

0 0 0
0 0 0
0 3 3
0 x 0
0 0 0
0 0 0
0 0 0
0 0 0
0 3 3
0 x 0
0 1 0
0 0 0

Part 2.3: Write a function with the following prototype:

int gameMenu (void);

The function should display the following menu, and return the menu selection as an integer. If an invalid entry is made, inform the user and continue to prompt for an entry.

0 - Quit the game.
1 - Show hits.
2 - Fire!!!
Your orders? -9
Invalid option.
Your orders? aaaaa
Invalid option.
Your orders? 2

In this example, gameMenu() would return 2.

Part 2.4: Write a function with the following prototype:

void playGame (int rows, int columns, int * boardPtr);

The playGame() function will need to do several things:

  • Declare an array to keep track of piece counts.
  • Initialize another board to keep track of hits and misses. You can do this using something like "fireData = calloc(rows * columns, sizeof(int));" and then "clearBoard(rows, columns, fireData);"
  • Enter into a loop that begins by calling gameMenu(). Based on the return value of gameMenu(), do the following:
    • On 0, break out of the loop.
    • On 1, use showBoard() to only show the fire data.
    • On 2, do the following (you may define your own function here to do all this, if you want):
      • Get target coordinates using getCoords().
      • Once valid coordinates are given, use fire() to update the board and the fire data.
      • If fire() returned 0, tell the user they missed.
      • Otherwise, tell the user they hit. Then use countPieces() to recompute the count of pieces. If the count of the pieces for the return value of fire() is 0, also print "You sank my %d's!\n", where %d is the piece number. If the return value of countPieces() was 0, tell the user they have destroyed everything, and break out of the game.
  • Clean up any memory allocated for the fire data.

Part 2.5: Modify your original main() definition so that the 3 menu option calls the playGame() function for the current board. When playGame() exits, it should return to the top level menu.

An example input/output session is available here. Remember to knock off objectives listed here one objective at a time, and not let the whole scope of the assignment overwhelm you! If you get stuck on something be sure to ask for help, and work on other objectives you know you can get. You may also find it useful to use functions like showBoard() to output debug data that will be removed before you turn in your assignment.

BONUS: 20 bonus points will be awarded if you modify your main() routine to handle variable size boards. For this, you will need to use either malloc() or calloc(), and ask the user to input the board size at the begining of the program. Board sizes should not exceed 26x26, nor be smaller than 2x2.

8/19Part 1 of the final assignment is available on its own page.
8/25The final assignment page has been updated with the rest of the assignment information.

While I don't believe in wasting paper, I feel it is important that detailed feedback be made available to students. For this reason, initial assignments should be submitted as printouts. This may change shortly, so keep a lookout for changes in submission policy.

Readability counts just as much as program correctness. For this reason, I require each program to have a comment header and a comment footer. The comment header should have the file name, your name, the course, the date the file was last edited, the date the file is due, and a description of the program. The footer should contain the name of the file. Every function must also have a comment block that explains the function name, its inputs, outputs and what the function's purpose is. For example:

/* ______________________________________________________________________
   File: example.c
   Name: Jonathan Riehl
   Course: CSPP 50101
   Date: June 21, 2004
   Due:  June 21, 2004

   This is an example.  Yep.
   I did this all by myself, but if I didn't, I'd note so here.


/* ______________________________________________________________________
   Include files

#include <stdio.h>

/* ______________________________________________________________________
   Function: main
   Inputs: None
   Outputs: Return code for program.
   Description: Main routine for the program.

int main (void)
    printf("Boy, this is a lot of garbage for a simple hello world program!\n");
    return 0;

/* ______________________________________________________________________
   End of example.c

Style penalties are listed below:

Missing or improper header/footer.-10 pts.
Missing or improper comment block-5 pts. each
Indentation mismatch-3 pts. each
Curly brace style mismatch-3 pts. each
Improper variable name-3 pts. each

The style notes I did for operating systems may also help illustrate just how much of a jerk I am. These notes will transition into doctrine when we move to programs with more than one file.

Academic, Honestly

Please note: The following was copied and adapted from an original I think came from Stuart Kurtz.

The University of Chicago is a scholarly academic community. You need to both understand and internalize the ethics of our community. A good place to start is with the Cadet's Honor Code of the US Military Academy: "A Cadet will not lie, cheat, or steal, or tolerate those who do." It is important to understand that the notion of property that matters most to academics is ideas, and that to pass someone else's ideas off as your own is to lie, cheat, and steal.

The University has a formal policy on Academic Honesty, which is somewhat more verbose than West Point's. Even so, you should read and understand it.

I believe that student interactions are an important and useful means to mastery of the material. I recommend that you discuss the material in this class with other students, and that includes the homework assignments. So what is the boundary between acceptable collaboration and academic misconduct? First, while it is acceptable to discuss homework, it is not acceptable to turn in someone else's work as your own. When the time comes to actually code your solution, you should type it yourself from your own memory. Moreover, you should cite any material discussions, or written sources, e.g.,

Note: I discussed this program with Jane Smith.

The University's policy, for its relative length, says less than it should regarding the culpability of those who know of misconduct by others, but do not report it. An all too common case has been where one student has decided to "help" another student by giving them a copy of their assignment, only to have that other student copy it and turn it in. In such cases, I will view both students as culpable and pursue disciplinary sanctions against both.

For the student collaborations, it can be a slippery slope that leads from sanctioned collaboration to outright misconduct. But for all the slipperiness, there is a clear line: present only your ideas as yours and attribute all others.

If you have any questions about what is or is not proper academic conduct, please ask me.