Vous êtes sur la page 1sur 3

Homework 01

Tedious Calculator
Due Monday September 19th by 11:59 pm
80 points
This project requires that you build a simple (if impractical) terminal-based calculator.
This calculator will accept user input of two integers, A and B. Once the user has input
these numbers, the program will display a menu of available commands (see Example
Input/Output below) consisting of various mathematical operations and a program exit
option. A user should select their options and see the computed results of their choice
or exit the program. The program should continue prompting the user for input until they
select exit or input an invalid menu item number.
User input/output (via cin/cout):
User should input 2 integers A and B >= 0.
Invalid integer input should be accounted for, resulting in a re-prompt for integer
input.
A menu must be presented to the user consisting of all possible commands.
The program should repeatedly prompt users for input of new A and B values
until a
user chooses to exit.
An invalid menu choice should NOT cause the program to exit.
You must implement the mathematical operations as functions:
Basic arithmetic (addition, subtraction, multiplication, division). You should check
(and prevent) division by zero errors.
Greatest Common Divisor (gcd): Find the largest integer value that divides both
A and B
leaving a remainder of 0. You can use Euclids algorithm.
Factorial: Compute n! for both A and B.
Square Root: You should write your own method for computing the square root of
A
number (you cannot use the math.h implementation.) The simplest algorithm is
the Babylonian method.
(OPTIONAL) Abs Value: Depending on how you implement the Babylonian
method, you might need this helper function (you cannot use the math.h
implementation.)
See the background information section below for specific help on implementing these
operations.
Code Requirements:
To receive full credit you must implement all features as outlined above, as well as fufill
the
following code specific guidelines:
Use at least one for loop
Use at least one while or do/while loop
Use a switch block or if/else block for menu commands

The only external dependencies allowed are cin/cout from <iostream>


gcd, sqrt, factorial and abs implemented as functions
Use recursion in one of the above functions

Example Input/Output:
>> Please enter an integer A: 2
>> Please enter an integer B: 16
Please select an operation:
1) Arithmetic
2) GCD
3) Factorial
4) Square root
5) EXIT
>> 2
GCD
GCD(2,16) = 2
>> Please enter an integer: 2
>> Please enter an integer: 16
Please select an operation:
1) Arithmetic
2) GCD
3) Factorial
4) Square root
5) EXIT
>> 4
Square Root
A: sqrt(2) = 1.41421
B: sqrt(16) = 4
>> Please enter an integer: 12
>> Please enter an integer: 124
Please select an operation:
1) Arithmetic
2) GCD
3) Factorial
4) Square root
5) EXIT
>> 5
Exiting!

Project Submission:
Remember -- each project requires that you provide a makefile or CMakeLists.txt in
addition to your source code. Archive your HW01 folder into a file (zip, .tar.gz). and
submit it on ICON/Canvas. I will provide a sample CMakeLists file online before
September 13th. Remember to make a git commit after a function or feature is
implemented. For 10 points extra credit you may provide unit tests, but this will not be
covered in class.

Grading:
This is a rough outline on how points will be assigned. Remember, if code doesnt
compile on the CSG machines included additional points will be taken off.
30 points: gcd, sqrt, and factorial implemented as functions with one
function implementation using recursion.
15 points: Branching and control statements used as outlined in requirements.
5 points: All code is well commented and syntax style (e.g., code blocks)
is consistent and easy to follow.
Background Information:
1. Factorial numbers:
The factorial of a number n, denoted n! , is the product of all positive integers less
than or equal to n. By convention, 0! = 1.
Example: 5! = 5*4*3*2*1
Note: With factorial computation, its quite easy to get numerical overflow errors
with even small numbers. 20! is the maximum factorial that can be computed using a
64-bit unsigned integer (value is 264 1 or 18,446,744,073,709,551,615)
2. Euclids Algorithm for finding the greatest common divisor of 2 numbers.
Recall that the greatest common divisor of two numbers is the largest number n that
divides both numbers with a remainder of 0. Euclids algorithm works using the
observation that given 2 integers A and B, subtracting the smaller integer from the
larger one doesnt change the gcd. For example, the gcd(120,100) == gcd(20,100).
Doing this process iteratively, gcd(120,100) == gcd(20,100) == gcd(20,80) ==
gcd(20,60) == gcd(20,40) == gcd(20,20) eventually reaches a point where both
numbers are equal. The resulting value is the gcd for the original values A and B (as
well as all intermediary pairs).
See the Wikipedia entry http://en.wikipedia.org/wiki/Euclidean_algorithm for
more information.
3. Babylonian Method for calculating the square root.
The first known algorithm for approximating the square root of a number S is the
Babylonian method. The insight to this algorithm is that if an initial guess x
overestimates the true value of ^0.5 then x/2 will underestimate the value of . Taking
the average of these two approximations will therefore provide a better approximation of
s^0.5 than our previous guess x. We can use this fact to continuously
improve our estimate of by iteratively updating x until the different between the
current guess x and our last guess x1 is no greater than some predefined threshold,
e.g., 0.0001.
See the Wikipedia entry
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_m
ethod for more information.

Vous aimerez peut-être aussi