Homework 1, assigned January 20-21

Homework summary

For homework for next lab, finish today's lab activities, do some reading in K&R, write two programs to be submitted online (bitCount and an adding machine), and complete an introductory survey.

This may seem like a lot of work to do in two days. We hope, however, that your previous experience with a C-based language will soften the impact of this assignment. (We're also assuming that you don't have much work yet in your other courses.)

Directions for online submission of your solutions to the bit-counting and adding machine programs will appear within a day or two.

Readings

Read the following in K&R for next lab:

A bitCount function

Write a function named bitCount that returns the number of 1-bits in the binary representation of its unsigned integer argument. Add your function to the following program, which is available online in ~cs61cl/code/bitcount.c, fill in the identification information, and run the completed program.

	#include <stdio.h>
	
	int bitCount (unsigned int n);
	
	int main ( ) {
		printf ("# 1-bits in base 2 rep of %u = %d, should be 0\n",
			0, bitCount (0));
		printf ("# 1-bits in base 2 rep of %u = %d, should be 1\n",
			1, bitCount (1));
		printf ("# 1-bits in base 2 rep of %u = %d, should be 16\n",
			1431655765, bitCount (1431655765));
		printf ("# 1-bits in base 2 rep of %u = %d, should be 1\n",
			1073741824, bitCount (1073741824));
		printf ("# 1-bits in base 2 rep of %u = %d, should be 32\n",
			4294967295u, bitCount (4294967295));
		return 0;
	}
	
	/* Your bit count function goes here. */

An adding machine

Write a C program named adder.c that simulates an adding machine. Input to the program will represent integer values, typed one per line without leading or trailing white space. (Don't worry about incorrectly formatted input.) Input should be handled as follows:

Your solution must use the getchar function to read input; it must not use scanf or any of its variations. You are forbidden to use arrays or strings. Don't provide any prompts; the only output your program should produce is lines of the form "subtotal __" and "total __". Don't identify yourself in the program code.

Example

user inputprogram's output
5
6
0subtotal 11
-3
-2
5
0subtotal 0
13
-13
8
0subtotal 8
0total 19

This problem is a bit tricky. In particular, you should make sure it works correctly when the first two input values are both 0.

Introductory survey

Complete a survey available online.