CS 99

Summer 2002: Lab 3                                                                        07.09

1.    Objectives

Completing all tasks in this assignment will help you:

·         practice with the while loop

·         write code that gives sensible answers even when the input data is empty

·         practice creating Boolean tests for novel mathematical situations 

First skim, then carefully read the entire assignment before starting any tasks.

 

2.    Geometric Mean
The geometric mean of a set of numbers x1 through xn is defined as the nth root of the product of the numbers:

               
geometric mean = (x1 x2 x3 … xn )1/n                           (1)

Write a MATLAB program that will accept an arbitrary number of possible input values and calculate the geometric mean of the numbers.  Use a
while loop to get the input values and terminate the input when a user enters a negative number.

What does your program do if the user enters a negative number as the very first value?  Is that what you want it to do? Can you fix it?

Test your program by calculating the geometric mean of the four numbers
10, 5, 2, and 25.

Save your script file as
GeoMean.m

3.    Not a perfect square
Demonstrate, for the first 10000 positive integers n, that

          
floor( n + sqrt(n) + 1/2 )                                                 (2)

is never a perfect square.  Hint: What test can you devise to determine if a number is a perfect square?

Your program should display a message only if (
2) is a perfect square for some n in the range.

Save your script file as perfectSquare.m.

4.    Submitting Your Work
Type your name (and your partner’s name if you have one), student ID, and the date at the top of each M-file.  Print each file and sign them along with your partner.  Give the signed documents to the teaching assistant at the end of the lab session.