Comp 1672
Winter 2003
Homework 2
Assigned Monday, January 13, 2003
Due Tuesday morning, January 21, 2003

1.  a. Convert 2356 to hexadecimal and then to binary
    First note that powers of 16 are 1, 16, 256, 4096, ...
    Next we decompose 2356 into sums of powers of 16
    2356 = 256*9 + 16*3 + 4 = 0x934 
    Now we convert each digit to binary separately
    0x934 = 100100110100(base 2)
    b. convert 0xCF8 to decimal
    0xCF8 = 12*256 + 15*16 + 8 = 3320
    (Check out the cool web site http://www.cut-the-knot.com/binary.shtml
    for an on-line conversion program.)

2. Read pp. 386-389, do problem 9 on page 401 (covers string processing)
   Here's the output of the program (explanation in parentheses):
   Going to the Amusement Park  (Just concatenate strings in the order given)
   14   (Just by counting the characters in str1) 
   10   (Because 'P' is the number 10 character in the string str1)
   musem   (By taking a string 5 characters long, starting at character 1)
   ABCDEFGHIJK  (str itself is just output to the screen)
   11   (Again, by counting the number of characters in string str)
   aBdDEFGHIJK  (the number 0 and number 2 characters were changed to a and d)

3. Do problem 6 on page 346 (reviews pass-by-reference)
   Here's the output of the program (explanation in parentheses):
   10 5 6 (mickey changes the first parameter (first) to be 2*second parameter,
           while the second parameter is pass by value, so it keeps the value 5,
           and x is also unchanged, at 6.)
   10 10 20 (minnie cannot change the value of the first parameter, so it keeps
            its value of 10 after the function call.  minnie calculates the new
            value of the second parameter to be 10, and main's copy of second
            is thus updated.  minnie has the perhaps undesired side-effect of
            changing the global variable x to be 10+10=20)

4. Do problem 11 on page 348 (default parameters)
   (i) okay, same as passing (5, 7, '*')
   (ii) okay, same as passing (5, 8, '*')
   (iii) Not okay, second parameter (if it's present) MUST be an int
   (iv)  okay, 3 parameters of the correct types are passed 

5. Do problem 12 on page 348 (default parameters)
   (i) same as passing 6, 5, 3.2, a comes out to be 19+16=35 (pay attention to
       when to do the casting)
   (ii) same as passing 3, 4, 3.2, a comes out to be 14+12=26
   (iii) a comes out to be 5+0=0

6. Find the error(s) in the following code:
   void process_array(const float a[], const int size)
   {
       int i, total(0), new_num;

       cin >> new_num;
       a[size++]=new_num;       // not okay, neither a nor size can be changed
       for (i=0; i < size; i++)
          total += a[i];
       for (i=0; i < size; i++)
          a[i] /= total;        // again, not allowed to change values of a
   }   
   
7. Do problem 4 on page 461 (character arrays)
   All output "Shelley" except for the last one, d.  The last example
   outputs ALL 8 characters stored in the array variable name, including
   uninitialized values past the null-terminator

8. Do problem 5 on page 461 (character arrays)
   a. strcpy(str1, "Sunny Day");
   b. length = strlen(str1);
   c. strcpy(str2, name);
   d. if (strcmp(str1, str2) <= 0)
         cout << str1;
      else
         cout str2;

9. Write your own version of the strcpy function
   Answers will vary quite a bit - Here's one possibility:
   void strcpy(char arr1[], char arr2[])
   {
       int i(0);
       while (arr2[i] != '\0') {  // examine each element of arr2 until 
           arr1[i] = arr2[i];     // the null terminator, then copy it
           i++;
       }
       arr1[i] = '\0';   // don't forget to null-terminate!
   }

10.Say I have the following definition:
   const int students = 3;
   const int exams = 4;
   int studentGrades[students][exams] =
       { {77, 68, 86, 73},
         {96, 87, 89, 78},
         {70, 90, 86, 81} };
   a. Write a few lines of code that will calculate the average score for
      each student.
      int i, j;
      float total;
      for (i=0; i < students; i++) {
           total = 0;
           for (j=0; j < exams; j++)
               total += studentGrades[i][j];
           cout << "Student " << i << " average: " << total/exams << endl;
      }
   b. Write a few lines of code that will calculate the average score on
      each exam.
      int i, j;
      float total;
      for (i = 0; i < exams; i ++) {
          total = 0;
          for (j=0; j < students; j++)
              total += studentGrades[j][i];
          cout << "Exam " << i << " average: " << total/students << endl;
      }