CMSC201
Programming Project One
Exponents
Out: Wednesday 9/26/07
Due: Wednesday 10/3/07, 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
value of nextGuess becomes the new value of 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 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 GetPositiveValue (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. It may be necessary to have initial
prompting done in the caling function that says what this value is, i.e. In this
example, the initial prompt might be, "Your Choice: ". See the sample output for
clarification.
- GetPositiveValue() - should prompt the user for a positive number, ensure that
it is a positive real number and return it. Like GetValidInt() this function should be
a module and so initial prompting may be necessary in the calling function to clarify what
the value is, and the prompt within the function should just ask for a positive value.
See the sample output for clarification.
- 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 value as its
single argument, n, and find the difference between the approximate square
root, calculated by your function FindApproxSqRt(), 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, therefore
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 Wednesday, 9/26/07
and Thursday, 9/27/07. Your project MUST be written as it is designed in
class.
Compiling the Pieces & Linking
- Separate compilation requires the -c flag to compile without linking :
gcc -Wall -ansi -c proj1.c
gcc -Wall -ansi -c exponents.c
- Since we are using the sqrt() function from the math library, we need to
use the -lm flag to explicitly link to the math library. None of the other
libraries we'll use will need explicit linking.
gcc proj1.o exponents.o -lm
Submitting the Program
To submit the files 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