CS61C Spring 2012 HW2

TA: Paul Ruan
Due Sunday, January 29, 2011 @ 23:59:59

Goals

This assignment is designed to give you some practice with C and a bit of review on binary and hexadecimal from section.

Submission

Copy the contents of `~cs61c/hw/sp12/hw2` to a suitable location in your home directory to obtain files you will want for this homework.

```	mkdir ~/hw2
cp -r ~cs61c/hw/sp12/hw2/ ~/hw2
```
In this directory, there should be files named `hw2.txt` and `bitcount.c`. Put your solutions to problems 1 and 2 in `hw2.txt`, and problem 3 in `bitcount.c`.
Then run `submit hw2`. Make sure both files are submitted.

Problem 1

a) Convert `10001001` in binary to decimal (under the unsigned number representation) and hexadecimal.

b) Convert `35` in decimal to binary and hexadecimal.

c) P&H (Revised 4th) exercise 2.33.2 for option b:
Convert this function into pointer-based code.

```	  void shift(int a[], int n) {
int i;
for(i = 0; i != n-1; i++)
a[i] = a[i+1];
}
```

Problem 2

a) Complete the following `setName`, `getStudentID`, and `setStudentID` functions. You may assume the pointers given are valid and not null.

```	#define MAX_NAME_LEN 127

typedef struct {
char name[MAX_NAME_LEN + 1];
unsigned long sid;
} Student;

/* return the name of student s */
const char* getName (const Student* s) {
return s->name;
}

/* set the name of student s
If name is too long, cut off characters after the maximum number of characters allowed.
*/
void setName(Student* s, const char* name) {
/* fill me in */
}

/* return the SID of student s */
unsigned long getStudentID(const Student* s) {
/* fill me in */
}

/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) {
/* fill me in */
}
```

b) What is the logical error in the following function?

```	Student* makeAndrew(void) {
Student s;
setName(&s, "Andrew");
setStudentID(&s, 12345678);
return &s;
}
```

Problem 3

a) Write a function `bitCount()` in `bitcount.c` that returns the number of 1-bits in the binary representation of its unsigned integer argument.
To compile `bitcount.c` and create an executable named `bitcount`:

```	gcc -std=c99 -o bitcount bitcount.c
```
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(void) {
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 17\n", 2863377066u, bitCount(2863377066u));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 268435456, bitCount(268435456));
printf ("# 1-bits in base 2 representation of %u = %d, should be 31\n", 4294705151u, bitCount(4294705151u));
return 0;
}

int bitCount(unsigned int n) {
}
```

b) You have decided that you want your bitcount program above to work from the command-line (see K&R Section 5.10), as follows:

```	# ./bitcount 17
2
# ./bitcount 255
8
# ./bitcount 10 20
too many arguments!
# ./bitcount
[the same result as from part a]
```
Edit your `bitcount.c` to include this functionality. You may assume that the single argument will always be an integer in the range from 0 to 231-1. You will find the function `atoi` helpful.

Extra for experts: Implement this exercise without using the library function `atoi` (or something comparable). (You don't actually get extra points for this).