CMSC201
Programming Project One
Exponents
Out: Tuesday 9/28/04
Due: Monday 10/4/04, Before Midnight
|
The Objective
The objective of this assignment is to get you started writing programs
in C in the UNIX environment. Its main emphasis is design, so we will be
designing the project together in class. This project will also give
you practice writing functions, loops, switch statements and using
separate compilation.
The Background
Although the square and cube of a number are very easy to calculate,
finding the square root of a number, or actually the approximation of the
squareroot , can be quite time consuming, especially if done by hand. Using a
computer, we can calculate square roots by using a successive approximation
algorithm. There are many such algorithms, but the one you will be using for
this project is shown below:
The square root of a number n can be approximated by repeated
calculation using the formula
nextGuess = 0.5 * (lastGuess + n / lastGuess)
where an initial guess of 1.0 will be the starting value of lastGuess.
A value for nextGuess should then be computed using the formula given
above. The difference between nextGuess and lastGuess should then be
checked to see if those two values are almost identical. If they are,
nextGuess is accepted as the square root of the number; otherwise, the
nextGuess becomes the lastGuess and the process is repeated (another
value is computed for nextGuess, the difference checked, and so on).
The loop should be repeated until the difference is less than 0.005
The Task
You are to write a program that calculates the square of a number, its
cube, and also its square root. Each of these tasks should be performed
in separate functions. The square root function that you write will return
an approximation of the square root of the number. You will also get the
precise square root of the number by using the sqrt() function found in the
math library. When finding the difference, you should report the absolute
value of the difference between your approximate square root and the square
root found by the function in the math library. When displaying the
difference, you MUST print it using exponential notation. Often the
difference is so small that using %f would just display 0.000000,
but the two roots are not really the same.
After printing the program explanation and getting a positive real
number from the user, you will display a menu that will allow the user to
choose which operations to do. The choices should include
- squaring the number
- cubing the number
- finding the approximate square root of the number
- finding the precise square root of the number
- finding the difference between the approximate and precise square roots
- getting a new number from the user
- OR quitting the program.
You will be expected to write eight functions, other than main, for this
project.
Here are the function prototypes:
void PrintGreeting (void);
void PrintMenu (void);
int GetValidInt (int min, int max);
double GetPositiveReal (void);
double FindSquare (double n);
double FindCube (double n);
double FindApproxSqRt (double n);
double FindDifference (double n);
You MUST use these function prototypes, as shown, without
modification.
Function Descriptions:
- PrintGreeting() - should print a message to the user, explaining
what the program will do. Since this function returns nothing, it can
also be called a procedure.
- PrintMenu() - should print the menu to the screen
- GetValidInt() - should prompt the user for an integer within the range
of min to max, inclusive, that are passed in as arguments, ensure the
integer entered is within that range and return it.
- GetPositiveReal() - should scan a number from the user, ensure that it
is positive and return it. It should only prompt if the user enters
an invalid number. (All other prompting should be done in the calling
function.)
- FindSquare() - This function will take a positive real number as its
single argument, n, and return n-squared.
- FindCube() - This function will take a positive real number as its single
argument, n, and return n-cubed.
- FindApproxSqRt() - This function will find the approximate square
root of a number, n, by performing repeated calculations using the formula
given above.
- FindDifference() - This function will take a positive real number as its
single argument, n, and find the difference between the approximate square
root (calculated by your function ApproxSquareRoot()) and the precise square
root (the value returned from the sqrt() function found in the math library).
It should compare these values and return the absolute value of the difference.
Sample Run
Since the output is long, here is a link to a separate webpage containing
the sample run.
More Details
- You MUST use the prototypes as shown without any modification.
- You MUST use separate compilation. Your files must be named
proj1.c, exponents.c and exponents.h, where
proj1.c should contain main(), exponents.c should contain the
function definitions and exponents.h should contain the function
prototypes for the functions found in exponents.c
- You MUST use symbolic constants for all numeric values, therefor
you will need the following #defines :
#define MIN 1
#define MAX 7
#define SQUARE 1
#define CUBE 2
#define APPROX_SQRT 3
#define SQRT 4
#define DIFFERENCE 5
#define NEW_NUM 6
#define QUIT 7
- The design for this project is to be given in lecture on 9/28/04
and 9/29/04. Your project MUST be written as it is designed in
class.
Submitting the Program
To submit the file you should use the command:
submit cs201 Proj1 proj1.c exponents.c exponents.h
You can check your submission by using the command:
submitls cs201 Proj1