FastPower - 15-121 Introduction to Data Structures Exercise...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121: Introduction to Data Structures Exercise 09/11/2009 Relevant Reading: • Chapter 7 in the Lewis and Chase book. • Section 9.1 of the online text. Topic: Recursion. Fast Power Fall 2009 Due: Mon, Sept 14 8am Assignment: (worth 5 exercise points) Start with the files in In this program, there is a power function which determines the value of xn, where x is a real number, and n is a positive integer. Your task is to replace the function ‘Power’ with a recursive function ‘FastPower’ which is defined as follows: if n = 0, then xn = 1 if n is even, then xn = x n/2 * x n/2 if n is odd, then xn = x n div 2 * x n div 2 * x, where n div 2 is n divided by 2, without any remainder. Test your function with some numbers you know the answer to. (It should give the same answers that the original Power function gave.) Written: Create a .txt file with your answers to the following questions: • How many multiplications did the original power function do for the input 28? • How many multiplications does your 'FastPower' function do for that same input? • How many multiplications does each function do for 3.14 16 and 1.1 512? You can determine the answers to these questions either by counting them up, or modifying your program to do so. Notes: • Be sure to put appropriate documentation (comments) in the program. Extra credit: Find a way to do actual timings of your code and run both methods. Create a report on which method is faster on which inputs. Handin: • Create a zip file containing all of the necessary java files. Submit this zip file via handin. If you have problems, contact me. ...
View Full Document

This document was uploaded on 12/19/2009.

Page1 / 2

FastPower - 15-121 Introduction to Data Structures Exercise...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online