CS61C Fall 2012 HW2

TA: Loc Do

Due Sunday, Sep 09, 2012 @ 23:59:59

Goals

This assignment is designed to give you some practice with C

Submission

Create a directory named “hw2” inside your working repository and put your solutions to problems 1 and 2 in hw2.txt, and problem 3 in bitcount.c. It is important that you place your submission for hw2 inside this directory and not somewhere else, as when we pull submissions, we will look for your submission there. Then run these commands:

git add −A
git commit −m “hw2 submission”
git tag −f hw2
git push --tags origin master
        

Problem 1

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.

#include <stdio.h>
#include <stdlib.h>
            
#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) {
        /* your code here */
    }
        

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).