TA: Jeffrey Dong, Riyaz Faizullabhoy
Due Sunday, September 15, 2013 @ 11:59 PM
Goals
This assignment is designed to give you some practice working with C.
In particular, getting familiar with pointers, arrays, bitmasking, strings, and command line arguments.
Obtaining the Assignment
Copy the hw1 files into your homework directory by typing the following. We recommend you do this in a repository
separate from your other assignments.
$ git pull ~cs61c/hw/02 master
For a refresher on git, please look at Lab 1 or the additional git notes!
Problem 1: Pointer-based Arrays
You are given the function shift in shift.c, which shifts a given array using array indexing operations.
Now, reimplement the same shift function, instead using pointer-based code (ie no indexing -- refer to slides/K&R for assistance).
Place your answer in hw2.txt.
Here is the shift function from array.c for reference:
/* * Sets the first n indices of array a to the original values of the indices one ahead of it * Ex: shift([1, 2, 3], 2) would return [2, 3, 3]) */ void shift(int a[], int n) { int i; for(i = 0; i != n; i++) a[i] = a[i+1]; }
Problem 2: Fix the Mistakes
The following segment of C code contains mistakes. For each mistake you find, please write down the line number
and correct the mistake (ie. write the correct C code). Correcting each mistake should require modifying only one line of code.
Put your answers in hw2.txt. You may not need all the blanks.
Line # Code
01 #include <stdio.h> 02 03 typedef struct node { 04 int var1; 05 int var2; 06 } point; 07 08 int sum_node_eles(point **my_node) { //returns the sum of members of a single node 09 int total; 10 total += my_node->var1 + my_node->var2; 11 return total; 12 } 13 14 int main(void) { 15 struct node *arr[5]; 16 struct point pt1 = {1,2}; 17 arr[0] = *pt1; 18 printf("The sum is %d.\n", sum_node_eles(arr)); 19 return 0; 20 }
Problem 3: Bitwise Operations
Complete the implementation of set_bit() and get_bit() in bitmask.c.
You may assume that we are working with 32-bit integers, so we will only ask you to set/get bits 0 to 31.
Bits are indexed from 0 to 31 starting with the least significant bit. For example, here is the same numbering
method applied to an 8 bit number 0b01101010 (the prefix 0b tells C that the number should be interpreted
as binary, not decimal):
Number: 0 1 1 0 1 0 1 0 Position: 7 6 5 4 3 2 1 0
We will not ask you to set a bit to any value other than 0 or 1, and you should not return a value other than 0 or 1 from get_bit(). We have included test cases in the main function. While you are free to test further, please make sure your main function matches ours, or the autograder will mark your solution as wrong.
Problem 4: Character Array String Rotation
- Given a string s and a non-negative integer n, rotate the string n places. The rotation is executed by moving the leftmost character to the rightmost character position of the string s, and then shifting all other characters one space to the left. Implement the rotate function in rotate.c. Test cases are included in the main function.
-
Now that you've implemented the rotate function, let's add command line arguments to main!
We will make the main function accept two arguments in the respective order: the string to be rotated, and the integer value of how many characters to rotate by. main will then call rotate with these inputs.
If no arguments are provided, the rotate function should just run the provided tests in main. You're free to add your own tests while you experiment writing the rotate function, but you must revert your changes back to the original tests, otherwise the autograder will mark your solution as wrong.
You must provide error messages for when the incorrect number of arguments are provided. You may safely assume that the two arguments passed in for rotation will always be a string and an int, in that order.
Hint, atoi might be helpful (if you don't know what it is, that's fine, search online!).
Example:# ./rotate hello 3 lohel # ./rotate hello Error: Incorrect arguments # ./rotate hello 5 hello Error: Incorrect arguments # ./rotate Should be: bookcoloring Results: bookcoloring Should be: sauceapple Results: sauceapple Should be: beanscool Results: beanscool Should be: 61cCS Results: 61cCS Should be: CS61c Results: CS61c
Submission
Please turn in your hw2.txt, bitmask.c, and rotate.c files. Do not submit other files.Run the submit command inside your hw2 directory with all of the aforementioned files.