Midterm 1, Fall 2004, CISC105, Post Exam Notes

These are some notes I came up with while grading the exams.

Even if you were satisfied with your performance on the exam, it may still be helpful to look over these comments, since in some cases I hint at how questions on future exams might be phrased. If you were disappointed in your performance, that's even more reason to look over these notes carefully.

This is not a complete answer key or rubric. For the questions that nearly every student got correct, I have not supplied answers here. Rather, I have supplied correct answers and comments on some incorrect answers only in cases where I think some additional comment might be helpful to the class' learning, or to CISC105 instructors in making up future exams on this material.

Questions 1-17

These were fairly straightforward; if you have questions about them, I'll go over them in class or during office hours. One note: I threw out question 1 since there was a typo, and some folks misunderstood my correction to the typo. As a result, you all got "two free points" on the exam.

Question 18

On this question, many students had perfect or nearly perfect solutions. Among the solutions that received less than stellar grades, there were several common problems:

Question 19

(10 pts) Write a C function (just a C function, not a complete program) called maxOfThree that takes three integers as parameters, and returns the maximum of those three integers.

Optional: If it will help you, you may write a function maxOfTwo first, and then write maxOfThree in terms of the maxOfTwo function. However, that is not required, nor will it earn you extra credit. It is just an option that you have.

Comments on this problem: Many students were able to answer this question with no trouble. However, I was disappointed at the number of students who still do not have a handle on what a "function" is in the C programming language. The question was very clear: it asked for a function, NOT a complete C++ program. A function takes its values from its parameters, and returns a value via the return statement.

Unless the instructions specifically tell you to prompt for a value, read a value or print a value, there should NEVER be a scanf or printf in a problem that asks you to write a function.

Let me repeat that for emphasis: unless the instructions specifically tell you to prompt, read or print, NEVER put a scanf or a printf inside a function! Unless told otherwise, use only the function parameters for input, and the only the return statement for output!

There are many possible correct answers to this problem. Here are just two of them:


int maxOfThree(int a, int b, int c)
{ if (a>=b && a>=c) return a; else if (b>=a && b>=c) return b; else return c; }

Here's another answer. Note that the instructions were specific: if you wanted to use maxOfTwo in your answer, you had to define it as well. That means a full function definiton must appear in your answer!

int maxOfTwo(int a, int b)
{
  if (a>b)
    return a;
  else
    return b;
}

int maxOfThree(int a, int b, int c)
{ return maxOfTwo(a, maxOfTwo(b,c)); }

Questions 21, 22

Number conversions:

  1. (4 pts) Convert 357 from decimal to binary

    Few students had any trouble coming up with the correct answer: 1 0110 0101.

    A common error was to forget one digit when counting up the digits from right to left; for example:

    256 64 32 16 8 4 2 1
    256 128 64 32 16 4 2 1
    256 128 32 16 8 4 2 1

    Each of the lists above is missing "one bit".

    Some students confused "decimal to binary" with "hexadecimal to binary". If the question asked you to convert from hexadecimal to binary, the answer would be 0011 0101 0111 (why?)

    What if the question asked for octal to binary? Then the answer would be 011 101 111 So it is very important to know what conversion is being asked for.

    A challenge for the next exam: how would you convert 73 from decimal to octal (answer: 111) ? How about decimal to hexadecimal (answer: 49)?

    See Chapter 1 for more help on this.

    Another possible question for the next exam: convert 2A from hexadecimal to decimal

    The correct answer is 42. "A" is in the one's place, and represents 10. "2" is in the 16's place, which means that it contributes "2 x 16 = 32" to the total. So the total is 32 + 10 = 42.

    When I gave this question on another exam, I noticed that some folks took "2" + "10" and came up with "12". Don't make that mistake!

  2. (4 pts) Convert the following from binary to hexadecimal:
    1010 1100 1010 1011 0100 1101 1010 1101

    Some of you made long computations here, and invariably, came up with an incorrect answer. This is actually a very simple question if you know the technique: take four bits at a time, and convert each to a digit (0-9, or A-F). This technique was covered in lecture. If you are still unsure of this, come by during office hours for a refresher course, or ask one of your classmates to help you; it only takes 5-10 minutes to learn it. Chapter 1 of your text may help also.

    The correct answer is: ACAB4DAD which might refer to a mode of transportation for a father that had consumed a bit too much of the flagship products of the establishment on Main Street, Newark Delaware, known as the "Iron Hill Brewery".

    Maybe we should have a competition to come up with the most clever use of the digits 0-9 and the letters A-F to spell out an message in exactly eight characters. If you come up with one, post it on your CISC105 web site (only in binary of course) along with a clue as to the interpretation. (We can relax the rules I suppose and allow message of more or less than 8 bytes, but it's nicer if they are 8 bytes since that is exactly the length of an "int" variable on strauss, i.e. 32 bits.)

    Here's one more, though this one is a 12 byte message: "If a Swedish 70s pop band all became vegetarians, Texas cattle ranchers might issue the following plea."

    1111 1110 1101 1101 1010 1011 1011 1010 1011 1110 1110 1111

    Finally a warning: occaisionally, I might try to be clever with my binary to hex conversions, but make a typo. If the binary seems to come "close" to spelling a message, but is one bit off, don't be deceived or doubt yourself; just translate the bits as they appear.

Question 23

  1. What is the output of each of the segments of C code below? Assume that each one occurs in the context of the main function of a valid, complete C program.

    Place your answer in the grid provided, one character or space per box. You might not need all the spaces in the grid provided.

    If you make a mistake, and want to start over, there are extra grids on the next page; just cross out the grid above, and indicate that your answer is on the next page.

    1. (5 pts)

      /* q1.c an example program that lacks comments */
      #include <stdio.h>
      int main (void)
      {
         int a, b, c, d, e;
         a = 5;
         b = 7;
         c = a * 3 - 4 * b;
         d = b / 2;
         e = a++; 
      
         printf("a=%d\n", a);
         printf("b=%d\n", b);
         printf("c=%d\n", c);
         printf("d=%d\n", d);
         printf("e=%d\n", e);
      
      }
      

      Correct Answer
      a = 5                                  
      b = 7                                  
      c = - 1 3                              
      d = 3                                  
      e = 5                                  

      Rubric: (-1) for each incorrect value, plus (-1) for bad spacing, or forgetting the "a=" on each line.

      Common mistakes included

      • adding extra space where it didn't belong
      • forgetting the "a=" etc on each line
      • incorrectly applying the order of operations to calculate c
      • forgetting that d is calculated with integer division, which throws away the remainder (and doesn't round)
      • forgetting that a++ "gives its value" to the expression before the variable a is incremented.

    2. (5 pts)

       

      /* q2.c Another program with no comments */
      #include <stdio.h>
      int main (void)
      {
        int count;
        count = 0;
        while (count <= 2)
        {
          printf("*");
          count++;
        }
        printf("\n");
        printf("Done\n");
        return 0;
      }
        

      Correct Answer

      * * *                                  
      D o n e                                
                                             
                                             



      Rubric for this item:
      • No response was (-5)
      • Skip an extra blank line: (-1)
      • Off-by-one error (2 or 4 stars instead of 3) (-1)
      • New line after every star (-1)
      • Upper case DONE (-1)
      • Printing numbers instead of stars (-3)
      • Extra spaces at start of line (-1)
      • Deductions of -5 or more, but at least got a star and a Done (+1)

Phillip T Conrad
Last modified: Thu Oct 14 18:21:11 EDT 2004