Homework 1

advertisement
Goals
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.
Background reading

K&R: Chapters 1-4.

P&H: Chapter 1, Chapter 3 sections 1 & 2.

Also be sure you understand Lab 1 Exercise 4.
Submitting Your Solution
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.
Problem 0
Copy the contents of ~cs61c/hw/01 to a suitable location in
your home directory.
$ mkdir ~/hw
$ cp -r ~cs61c/hw/01/ ~/hw
All the files referred in this homework will be in the folder
you copied.
Problem 1
In cod.txt, answer the following questions:

P&H problems 1.1-1.45 (these are very short questions),
1.48, and 1.54.

P&H problems 3.1-3.3
Problem 2
What are the decimal values of each of the following binary
numbers if you interpret them as 2's complement
a. 1111 1111 0000 0110
b. 1111 1111 1110 1111
c. 0111 1111 1110 1111
d. 0101 0101 0101 0101
Add your answers in cod.txt.
integers?
Problem 3
What are the decimal values of each of the following binary
numbers if you interpret them as unsigned
integers?
a. 1111 1111 0000 0110
b. 1111 1111 1110 1111
c. 0111 1111 1110 1111
d. 0101 0101 0101 0101
Add your answers to cod.txt.
Problem 4
For each of the bit lengths and number representations below,
list the binary encodings and values of the possible numbers
closest to +infinity and to -infinity. (For each of the three
parts to this question, you will provide two binary strings
and two corresponding decimal numbers.)
a. A nibble using two's compliment.
b. A byte using sign-magnitude.
c. A byte using one's compliment.
Add your answers to cod.txt
Problem 5
Write a function named
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
0\n",
0, bitCount (0));
printf ("# 1-bits in base 2 representation
1\n",
1, bitCount (1));
printf ("# 1-bits in base 2 representation
16\n",
2863311530u, bitCount (2863311530u));
printf ("# 1-bits in base 2 representation
1\n",
536870912, bitCount (536870912));
printf ("# 1-bits in base 2 representation
32\n",
4294967295u, bitCount (4294967295u));
return 0;
}
int bitCount (unsigned int n)
{
of %u = %d, should be
of %u = %d, should be
of %u = %d, should be
of %u = %d, should be
of %u = %d, should be
/* your code here */
}
Problem 6
Write a basic calculator program which takes three commandline arguments (see K&R Sec. 5.10) and evaluates them as an
arithmetic expression. The command-line arguments will
consist of two integers (representable by an
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
functions
arguments..
atoi
(Hint:
and strcmp useful,
you
may
as
well
find
as
a
the
case-
switch
statement.) Extra for experts: Extend your program to
take
an
follows:
arbitrary
number
of
arguments,
./mycalc int op int op int op int ...
given
as
Unless you are
feeling particularly ambitious, we encourage you to use one
level of operator precedence and left to right associativity.
Download