



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The cs61c midterm exam from the university of california, berkeley for the spring 2007 session, instructed by dr. Dan garcia. The exam covers various topics in electrical engineering and computer sciences, including data representation, number systems, memory management, and mips assembly language.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




After the exam, indicate on the line above where you fall in the emotion spectrum between āsadā & āsmileyā...
All the work is my own. I had no prior knowledge of the exam contents nor will I share the contents with others in CS61C
Instructions (Read Me!)
Question 0 1 2 3 4 5 Total
Minutes 1 36 36 36 36 36 180
Points 1 15 14 15 15 15 75
Score
Question 1: Is this the best midterm in memory? No, we freed it! (15 pts, 36 min)
Algorithm: SUM = encode( decode-into-decimal(A) + decode-into-decimal(B) )
50
1 typedef struct bignum { 2 int len; 3 *char num; 4 char description[100]; 5 } bignum_t; 6 *bignum_t res; 7 8 int main() { 9 bignum_t b; 10 *b.num = (char ) malloc (5 * sizeof(char));
// more code below
Question 3: Goodness, Grandma, what bignums you have! (15 pts, 36 min)
4
#define POS '+' #define NEG '-' typedef struct sci_bignum { char sign; // POS or NEG *char significand; // null-terminated string of decimal digits ('.' implicit) unsigned int num_sigfigs; // equal to strlen(significand) int exponent; } sci_bignum_t;
// Compare a to b; return <0 if a < b, 0 if a == b, or >0 if a > b (just like strcmp )
**int sci_bignum_cmp(sci_bignum_t a, sci_bignum_t b) {
if (a->exponent != b->exponent) {
return (a->exponent < b->exponent? -1 : 1);
} else if (a->sign != b->sign) {
return (a->sign < b->sign? -1 : 1);
} else {
for(int i = 0; a->significand[i]; i++) {
if(a->significand[i] != b->significand[i])
return (a->significand[i] < b->significand[i]? -1 : 1); } } }
Bug Description
Values for a and b that reveal the bug
What a correct sci_bignum_cmp should return
What this buggy sci_bignum_cmp returns / does a =
b =
a =
b =
a =
b =
a =
b =
4 , the sign would be the char '+', the significand would be the null-terminated string "123", num_sigfigs would be 3 , and the exponent would be 4. There is an implicit decimal point after the first significand digit.
Question 3: Goodness, Grandma, what bignums you have! (contād)
*typedef struct sci_vector { sci_bignum_t elts; unsigned int num_elts; } sci_vector_t;
#define MIN(a, b) ((a)<(b)?(a):(b))
*void clean_vector(sci_vector_t vec) { unsigned int min_sigfigs = 0xFFFFFFFF; // Initialize to biggest unsigned int
/* get min significant digits */ for (unsigned int i = 0; i < vec->num_elts; i++)
min_sigfigs = MIN( _________________________ , ____________________________);
/* truncate all elts to have min_sigfigs */ *for (unsigned int i = 0; i < vec->num_elts ; i++) { sci_bignum_t b = _____________________________ // convenient reference
*if (______________________________ > ______________________________) { char new_significand = (______) malloc(______________________________); _________________________________________________________ // for strcpy
strcpy(new_significand, b->significand);
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
} } }
Question 5: Heās a unix. Heās definitely a unix. Heās dead! (15 pts, 36 min)
unix% count_argument_characters 0
unix% count_argument_characters I love cs61c! 11
main: ______________________ # ans=
word: addiu ____________, -1 # Decrement
beq ____________, done # Weāre done!
______ $t0, ____, ____ # change $t
letr: ______________________ #
beq $t2, _______, word # end of word
addiu $t1, $t1, 1 # increment $t
j ____________________ # keep processing
done: jr $ra