Assignment 1, Solutions
Part of
the homework for 22C:60 (CS:2630), Spring 2012
|
int funct( int i ) { if (i > 3) { i = funct(i - 2) + funct(i - 3); } return i; }
Problem: Make a table of the values of funct(i) for values of i ranging from 0 to 10. (1 point)
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 3 5 6 8 11 14 19
char digit_a( int i ) { const char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; return digits[i % 10]; } char digit_b( int i ) { return (i % 10) + '0'; }
a) For what values of the parameter i would you expect these functions to return different values, and for what values of i do they return the same thing? (1 point)
They should behave the same for non-negative values.
b) For the values where these two functions agree, what do they do? (1 point)
For values of i from 0 to 9, digit(i) returns the character representing the value of the number. In general, for positive values of i, digit(i) returns the character representing the least significant digit of the decimal representation of the number.
01010011 S
01110000 p
01110010 r
01101001 i
01101110 n
01100111 g
00100000
00110010 2
00110000 0
00110001 1
00110010 2