CS 61C, Spring 2008
Due Friday, February 1, 2008 @ 11:59pm
Last Updated 1/27 @ 1pm
This assignment checks your understanding of the material in P&H Chapter 1 as well as some number representation basics. It will also give you practice compiling and executing C programs.
Submit your solution by creating a directory named hw1 that contains files named cod.txt, bitcount.c, and mycalc.c. (Note that capitalization matters in file names; the submission program will not accept your submission if your file names differ at all from those specified.) From within that directory, type "submit hw1". Partners are not allowed on this assignment.
$ mkdir ~/hw $ cp -r ~cs61c/hw/01/ ~/hwAll the files referred in this homework will be in the folder you copied.
bitCount()
in bitcount.c
that returns the number of 1-bits in the binary representation of its unsigned integer argument. Remember to fill in the identification information and run the completed program to verify correctness.
/* Name: Lab section time: */ #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 16\n", 2863311530u, bitCount (2863311530u)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 536870912, bitCount (536870912)); printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n", 4294967295u, bitCount (4294967295u)); return 0; } int bitCount (unsigned int n) { /* your code here */ }
int
) and a string ("plus", "minus", "times", or "over") in the order int opstring int
. Calls to the program should work as below:
# ./mycalc 5 minus 2 3 # ./mycalc 10002 times 5 50010 # ./mycalc 5 over 2 2 # ./mycalc 10 100 donkey donkey too many arguments! # ./mycalc too few arguments!Your code should be in a file named
mycalc.c
. We will not be testing for malformed input other than supplying too many or too few arguments.. (Hint: you may find the functions atoi
and strcmp
useful, as well as a case-switch
statement.)
Extra for experts: Extend your program to take an arbitrary number of arguments, given as follows: ./mycalc int op int op int op int ...
Unless you are feeling particularly ambitious, we encourage you to use one level of operator precedence and left to right associativity.