Assignment 1, Solutions

Part of the homework for 22C:60 (CS:2630), Spring 2012
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Homework questions based on prerequisite material:

  • Background: Consider this function in C (but C++, Java and C# look very similar):
    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)

    012345678910
    01233568111419

  • Background: Consider these functions in C (but C++, Java and C# look very similar):
    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.

    Homework questions based Chapter 2 of the text:

  • A Problem: Give the 7-bit ASCII representation of the text "Spring 2012" Don't include the quotes. Give your result as a column of binary numbers. (1 point)

    01010011 S
    01110000 p
    01110010 r
    01101001 i
    01101110 n
    01100111 g
    00100000
    00110010 2
    00110000 0
    00110001 1
    00110010 2