CISC 181 Introduction to Computer Science, Fall, 1998

Oct 26 Lab project -- fibonacci, and exponentiation

  1. To do in lab

    1. Make a copy in your directory of 181/lab7/fibonacci.cc.
    2. Compile it and give it the name fiblong
       
      CC fibonacci.cc -o fiblong
      
      Now for example fiblong 5 prints the first five fibonacci numbers, 1,1,2,3,4. In general fiblong n prints a list of the fibonacci numbers through fibonacci( n ).
    3. Run it with several values of n and find the first value of n for which it produces an incorrect result (due to long integer overflow).
    4. Modify the code so that fibonacci returns a double instead of a long . Be sure variables which hold partial results are also double.
    5. compile and give it the name fibdouble
      CC fibonacci.cc -o fibdouble
      
    6. Run it with several values of n and find the first value of n for which it produces an incorrect value due to double precision floating point overflow. What is that incorrect value.
    7. print your double version of the fibonacci.cc. Write on it the index and value of the first incorrect value for fiblong and for fibdouble.
  2. Lab extra credit: Observe that the iterative version of fibonacci computes the prior fibonacci numbers in order along the way. Thus for generating the table of fibonacci numbers up to n. We could just call fibonacci(n) once, but have it modified so as to print each fibonacci number along the way. Make this modification (on the double precision version).
    1. How many times is the while loop in fibonacci executed with the original version if the input number is 1000?
    2. How many times is the while loop executed with your modified version if the input number is 1000?
    3. WITHOUT having the computer report it, figure out the answer to the two previous questions if 1000 is replaced by 100.
    4. Hand in printout of code and a brief report of the experiments and calculation. This may be done later in the week.
  3. Computing assignment for next week. Due Monday in lab.
    1. The file 181/lab7/isPrime.cc contains an incomplete program for testing whether a positive number is a prime. Complete the program by implementing the function expMod.
    2. Hand in a printout of your code and also a printout of a session of test runs. You can use script, which will be explained in lab.