CS 1073 Engineering Programming
Answers to Questions for
Exam 1, Fall 2005


Date and length: Friday, 7 October 2005. The full 50-minute period: 3:00 pm - 3:50 pm. Answers are in red.


Sample Questions: I will pick simple questions about many of the topics from the web pages and from class. Often it will involve giving you some code and asking you to explain, modify, or complete the code. Here are topics and sample questions.


Numbers:

  1. Write the number -342.81 using the C floating point notation, and starting with -3.4...

  2. What is the binary number 101101 equal to as a decimal number?
    101101 = 1*232 + 0*216 + 1*28 + 1*24 + 0*22 + 1*21 = 32 + 8 + 4 + 1 = 45

  3. What is the C hexadecimal constant 0x2d equal to at a binary number? As a decimal number?
    0x2d = 2*16 + 13*1 = 45 (base 10) = 101101 (base 2)


Operators and expresions in C:

  1. In each case give the value of the C expression:


if-else in C:

  1. Write a segment of code that will start with three double variables a, b, and c, and will compute the double d = b2 - 4 a c . Then an if-else should check and print either "real, not equal", "real, equal", or "complex", depending on the value of d.


switch in C: No questions about the switch statement on this exam.


Input/Output using printf and scanf:

  1. Fill in the rest of the printf so that it will produce the output show:

  2. Write a short code segment that will declare two doubles, will read them, will calculate the smaller of the two, and finally will print the smaller value.


Loops:

  1. Consider the infinite loop that was presented in class:

    Explain why this is an infinite loop. See Lecture 4 for an answer.

    Add code that uses a counter to stop the loop after 25 iterations. (Do this without looking up the answer.) See Lecture 4 for an answer.

  2. Write a loop that adds up the odd integers from 1 to 49 and prints the result. See Lecture 4 for an answer.

  3. Work on the examples from Practice with Loops. (Altogether there are 10 short problems here.) The answers are included at the link.


Functions:

  1. Write a function named disc that will take three input double parameters a, b, and c, and will compute b2 - 4 a c and return this value. Show a call to this function using parameter values 1.0, -4.0, and 4.0.

    Make use of this function in the earlier example involving the same type of data.

  2. Write a C program segment that will read an integer n using scanf, and then will print a hollow triangle of stars of height n, as shown below for n == 10 and n == 5. (Assume that n > 1 The program needs to work for general n and not just 10 and 5, so you must use a loop in the program.)
    10
    *
    **
    * *
    *  *
    *   *
    *    *
    *     *
    *      *
    *       *
    **********    
    
    5
    *
    **
    * *
    *  *
    *****    
    
    13
    *
    **
    * *
    *  *
    *   *
    *    *
    *     *
    *      *
    *       *
    *        *
    *         *
    *          *
    *************    
    
    3
    *
    **
    ***    
    
    2
    *
    **    
    
    8
    *
    **
    * *
    *  *
    *   *
    *    *
    *     *
    ********
    

    Here, the format code %c is used to print a single character, so that chars(5, '*') will print 5 stars with no newline, and chars(4, ' ') will print 4 blanks with no newline.

  3. Using the same methods as the previous problem, try to print a "tree" of the following form, for different initial values of n.

    8
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************    
          ***
          ***
          ***
    
    6
         *
        ***
       *****
      *******
     *********
    ***********    
        ***
        ***
        ***
    
    10
             *
            ***
           *****
          *******
         *********
        ***********
       *************
      ***************
     *****************
    *******************    
            ***
            ***
            ***
    
    3
      *
     ***
    *****    
     ***
     ***
     ***
    
    12
               *
              ***
             *****
            *******
           *********
          ***********
         *************
        ***************
       *****************
      *******************
     *********************
    ***********************
              ***
              ***
              ***
    


Random Numbers:

  1. Suppose rand_double() returns a uniformly distributed random double between 0 and 1. Use this function to similate rolling three dice at a time. Keep rolling until you roll three 1's, counting as you go. Print how many rolls it took until you rolled the three 1's.

    Here are three versions of the basic loop in this example:

  2. Redo the program so that it will keep rolling 3 dice until all three dice come up with the same number (not necessarily just 1). The print how many rolls it took to roll the same number on all three dice.


Arrays: Hold questions until the next exam.