CSCI 1300
Introduction to Computing


Quiz #2

1. Write C++ expressions for the following:
    Example: For "p equals 5" you would write "p = = 5".

  1. x is greater than 7
  2. x > 7

  3. y is between 9 and 16

    (y > 9) && (y < 16) or (y >= 9) && (y <= 9)

  4. z is less than or equal to 0 or greater than 100

    (z <= 0) || (z > 100)

 

2. Write C++ code that uses two for loops (one inside the other) to
   print out the multiplication table for the numbers 1 through 10
   (i.e. it should print out 1*1 = 1, 1*2 = 2, …, 10*10 = 100).

for(i = 1; i <= 10; i++)
    for(j = 1; j <= 10; j++)
       cout << i << " * " << j << " = " << i * j << "\n";

 

3. Given two integers x and y where x = 110111012 and y = 101010102, what is:

  1. x & y

    10001000

  2. x | y
  3. 11111111

  4. x + y
  5. 10000111

  6. x – y
  7. 00110011

  8. x >> 3
  9. 00011011

 

4. Fibonacci numbers are a sequence of numbers discovered by Fibonacci around 800 years ago.
   The sequence is defined as follows: the first and second Fibonacci numbers are 1.
   Every other Fibonacci number is defined to be the sum of the previous two Fibonacci numbers.
   So, the sequence begins 1, 1, 2, 3, 5, 8, 13, …

   Given an array as follows:

int list[50];

   Write a C++ code fragment (i.e. not a whole program) that fills the array with the first
   50 Fibonacci numbers, stopping when all 50 Fibonacci numbers have been computed
   or when a Fibonacci number has been computed that is greater than MAXINT/2.
   [Note: MAXINT is a constant defined in values.h]

   Hint: start by setting the first two elements of the array, then use a for loop to compute the rest.

list[0] = 1;
list[1] = 1;

for(i = 2; i < 50; i++) {
    list[i] = list[i-1] + list[i-2];

    if(list[i] > MAXINT/2)
       break;
}