CS61C Summer 2013 Lab 1 - C Intro, Number Representation

Goals

Reading

Accounts

You need to have an EECS instructional account (cs61c-**) in order to do your lab work. Most importantly, you will need to log in to this account in order to enroll in the class. Exercise 0 below will guide you through setting up your account.

Policies and Partners

Please look over the lab policies in the course syllabus to make sure you know what is expected of you.

We are REQUIRING that you have a partner for lab checkoffs. This will reduce the number of check-offs we have to perform as well as give you someone to discuss class material with. BOTH partners will need to be present at check-off to receive credit and both partners will be asked to participate during the check-off. Try your best to find someone in your lab section with similar work habits as yourself.

How Checkoffs Work

You'll notice that at the end of (almost) every exercise, there is a section labelled "Check-off." The items in this section are what you must successfully demonstrate to your TA in order to receive credit for completing the lab. Once you and your partner finish ALL of the exercises, you should put your names AND logins on the checkoff list on the board, and a TA or Lab Assistant will come and check you off.

Extra Credit Opportunity: Excluding this lab (lab01), if you complete a lab before your assigned lab time and get it checked off in the first hour of your regular lab, you will receive one extra credit point on the lab (a 5 out of 4).

Exercises

Exercise 0: Account Setup

Once your TA gives out account forms, both you and your partner should do the following:

  1. Log into your account using the details on the form. You should be prompted to enter some personal information (name, SID) so that we can accurately assign grades at the end of the semester.
  2. If you were not prompted to enter your information in the last step, run register in Terminal. (Also, please alert your TA if this happens.)
  3. Finally, you should change your password. In Terminal, enter ssh update.cs.berkeley.edu and follow the prompts.
  4. Now you're ready to start the lab! Go ahead and run the following to copy the necessary files into the ~/lab01 directory:
$ mkdir ~/lab01
$ cp ~cs61c/labs/01/* ~/lab01/.

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

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

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

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:

  1. 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?
  2. Based on the value of the largest positive signed long, how many bits does the C data type long have on your current machine?
  3. 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?
  4. 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