Goals
- Practice with binary numbers and IEC prefixes.
- Learn how to compile and run a C program on the EECS instructional computers.
- Examine different types of control flow in C.
- Look at the internal representation of numbers.
Setup
Copy the contents of~cs61c/labs/su14/02
to your home directory. E.g.
$ cp -r ~cs61c/labs/su14/02 ~/lab02
Exercises
Exercise 1: Practice with Numbers
This exercise can be done WITHOUT a lab account!
Part 1: Cisco Binary Game -- The object is to match the number on the right with the unsigned binary representation on the left. This requires a combination of clicking and typing. And then there's time pressure to make things more fun! The game gets progressively faster and more difficult are you progress. See if you can score over 20,000 points and get to Level 5!
Part 2: IEC Prefixes -- Practice converting between powers of 2 and IEC prefixes (in both directions!) until you can do so consistently and relatively quickly. This will help you a lot later on in the course!
Check-off
- Score over 18,000 points on the binary game and show your TA (pause the game or take a screenshot)!
- Your TA will give you and your partner a random power of two and a random IEC prefix (one to each of you). Convert it correctly in under 5 seconds.
Exercise 2: Simple C program
The following C program, also in output0.c, is supposed to output two zeros but currently doesn't work! Make changes to the two lines specified to produce the desired behavior. Don't change anything else in the program. The following references may help: ASCII Table and printf.
#include <stdio.h> int main(void) { int n; n = 0; /* fix 1: only change this line */ printf("fix 1: %c\n",n); n = 0; printf("fix 2: %c\n",n); /* fix 2: only change this line */ return 0; }
To verify your solution, compile it and run the resulting executable:
$ gcc -o output0 output0.c $ ./output0 fix 1: 0 fix 2: 0
Note: In general the -o "NAME" tag specifies the name of your executable. Without it (i.e. gcc output0.c), the executable defaults to a.out.
Check-off
- Show your code to your TA and explain the changes you made to output0.c.
Exercise 3: C control flow
Berkeley is well-known for its academics as well as its eccentrics! Look at the code contained in eccentric.c. In it are four different examples of basic C control flow. Compile and run the program to see what it does. Modifying ONLY the initialization values of variables, make the program produce the following output:
$ gcc -o eccentric eccentric.c $ ./eccentric Berkeley eccentrics: ==================== Happy Happy Happy Yoshua Go BEARS!
Check-off
- Show your modified code to your TA and explain the changes you made.
- If you are allowed to replace instances of the control variables (i,j,k,m) with each other in the code, what is the minimum # of variables you could use to produce the same output above?
Exercise 4: The Biggest Integer
In class we discussed number representation. In particular, we discussed unsigned integers and two's complement, the almost ubiquitous format for signed integers. Look at biggestInt.c. You may wish to read through the comments but at this point it is not critical that you understand exactly how the program works. Basically, it is a C program that will tell you some useful information about certain C data types. It does this by exploiting the fact that C does not check for overflow and wrap around conditions. Compile and run the program and use the provided information to answer the following questions:
- Based on the value of the most significant bit (MSB) of an unsigned int, how many bits does the C data type unsigned int have on your current machine?
- Based on the value of the largest positive signed long, how many bits does the C data type long have on your current machine?
- Based on the value of the most negative signed int, do the unsigned int and signed int have the same number of bits on your current machine?
- Two's complement number representations have one more negative number than they do positive numbers (i.e. the most negative number does not have a positive counter-part). The final piece of information printed is the signed value of what you get when you try to negate the most negative value. Why does this happen?
Check-off
- Show the output of biggestInt.c to your TA (results will vary between different machines).
- Give your answers AND your reasoning to the above four questions.