Finish the readings assigned from K&R, then write two programs to be submitted online (bitCount and an adding machine).
To submit your assignment, create a directory called hw1 containing the solutions to both parts of the assignment. You should have one file called adder.c and one called bitcount.c. From this directory, run 'submit hw1'.
Read the following in K&R for next lab:
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 ~cs61c/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 representation of %u = %d, should be 0\n", 0, bitCount (0)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 1, bitCount (1)); printf ("# 1-bits in base 2 representation of %u = %d, should be 8\n", 9751808u, bitCount (9751808u)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 16777216u, bitCount (16777216u)); printf ("# 1-bits in base 2 representation of %u = %d, should be 8\n", 624115712u, bitCount (624115712u)); return 0; } int bitCount (unsigned int n) { /* your code here */ }
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.
| user input | program's output |
| 5 | |
| 6 | |
| 0 | subtotal 11 |
| -3 | |
| -2 | |
| 5 | |
| 0 | subtotal 0 |
| 13 | |
| -13 | |
| 8 | |
| 0 | subtotal 8 |
| 0 | total 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.