This assignment will give you practice running compiling and executing C programs.
You must submit a solution to this assignment to remain in CS 61C.
K&R: Sections 1.1-1.5, 1.7, 1.8, all of chapter 2 except material on arrays, sections 3.1-3.7 except material on arrays, and sections 4.1 and 4.2.
Submit your solution online by 8pm on 27th June 2004. Do this by creating a directory named hw1-1 that contains files named base2.print.c and bitcount.c. From within that directory, type
submit hw1-1This is not a partnership assignment ; hand in your own work, and don't collaborate with anyone else.
By convention, files containing C code have names ending in ".c".
To compile and run a C program named hw1.c, type
gcc -Wall -g hw1.cto the UNIX shell. This creates an executable file named a.out, which you then may run by typing
./a.outto the shell. You may give the executable file a different name either by renaming it with the mv command or by specifying the name in the gcc command with the "-o" option. For example, the following command creates an executable file named hw1.
gcc -Wall -g hw1.c -o hw1The option -g produces debugging information which GDB can work with. -Wall enables all warnings. More information about gcc options is available at GCC Online Docs.
If you run in to problems, use the class newsgroup to ask questions.
Put solutions to this problem in a file called prob1.txt. This is to help you guys warm up with number representations.
Do problems 4.1-4.8 on pages 322-323 in P&H
Also Convert 1495 (base 10) into a base 17 representation.
The program ~cs61c/lib/buggy.base2.print.c (listed below) is intended to print the binary (base 2) representation of the unsigned value stored in the variable numToPrintInBase2. It has bugs. Fix them, create a file named base2.print.c by changing no more than three lines in buggy.base2.print.c. Also fill in the identification information at the top of the file. Don't delete or add any lines.
/*
Name:
Lab section time:
*/
#include <stdio.h>
int main ( ) {
unsigned int numToPrintInBase2 = 1431655765; /* alternating 1’s and 0’s*/
unsigned int exp = 1;
int k; /* can't declare variable in a loop header */
/* Compute the highest storable power of 2 (2 to the 31th). */
for (k=0; k<31; k++) {
exp = exp * 2;
}
/* For each power of 2 from the highest to the lowest,
print 1 if it occurs in the number, 0 otherwise. */
for (k=31; !(k=0); k--) {
if (numToPrintInBase2 >= exp) {
printf ("%d", '1');
numToPrintInBase2 = numToPrintInBase2 - exp;
} else {
printf ("%d", '0');
}
exp = exp / 2;
}
printf ("\n");
return 0;
}
Write a function named bitCount that returns the number of 1-bits in the binary representation of its unsigned integer argument. Add your function to the following program, which is available online in ~cs61c/lib/bitcount.c, fill in the identification information, and run the completed program.
/* Name: Lab section time: */ #include <stdio.h> int bitCount (unsigned int n); int main ( ) { printf ("# 1-bits in base 2 rep of %u = %d, should be 0\n", 0, bitCount (0)); printf ("# 1-bits in base 2 rep of %u = %d, should be 1\n", 1, bitCount (1)); printf ("# 1-bits in base 2 rep of %u = %d, should be 16\n", 1431655765, bitCount (1431655765)); printf ("# 1-bits in base 2 rep of %u = %d, should be 1\n", 1073741824, bitCount (1073741824)); printf ("# 1-bits in base 2 rep of %u = %d, should be 32\n", 4294967295, bitCount (4294967295)); return 0; } /* Your bit count function goes here. */