Lab 3: Functions

The objectives for this assignment are for you to:
• Understand how to create and how to use functions.
• Develop proper habits of dividing a problem into separate parts.
• Strengthen your understanding and proper use of conditional structures and loop structures.
• Notes:
- in this lab, all functions you write must pass arguments by value, not by reference (we will learn passing by reference later).
- all programs with functions must have the function prototypes at the start of the program (under the #include's, before main), and must have the function definitions placed after and under the main section (any program whose function definitions are written above main will not be accepted this semester).

In-Lab Exercise

Here is a simple program to find the perimeter and the area of a rectangle, given its length are width that are given by the user.

Based on the above, complete this program so that the tasks are done inside functions, which are called from main().

Demo the program to a TA.

Getting Started

As in previous labs, make sure you create the necessary directories in both your home directory and in your dropbox.

Part 1: simple function

In this first part, write a program, to be named gcdmain.cpp, that asks the user to enter two integer numbers and that then finds and displays the greatest common divisor between them. The program must contain a function called getgcd() which takes two int's as input and returns their gcd as an int for output.
The function prototype must therefore be:   int getgcd(int, int);

You may use any non-recursive gcd algorithm (but not the code) that you find from a book or online.
You may assume that the user will only enter positive integer numbers, so no need to check.

Part 2: quadrants and polar coordinates

Write a program, named polar.cpp, that prompts the user for the x and y coordinates (of data type double) for a point in a Cartesian coordinate system (plane), and that then finds and displays the following:
• the point's corresponding polar coordinates: radius, and angle, in degrees
• the quadrant that the point is in; or whether the point is on an axis (specify which one), or if it's at the origin, as the case may be
• You must write at least three functions to determine the above; their prototypes are:
- double get_radius(double, double);
- double get_angle(double, double);
- void quadrant_info(double, double);

Note that the third function acts as a procedure, where the info is computed and displayed from within. Refer to the related class discussion.
You may assume that the user will always enter numeric values.
Feel free to write any other functions that you feel may be useful.

Hint: you will obviously need to use an inverse tangent function; the cmath library has two of them: atan and atan2; look them up to see the difference, and choose the one you feel helps you best.

Part 3: text menu calculator

Write a program, named menucalc.cpp, that will act as a text menu-driven basic calculator for the four simple arithmetic operations: add, subtract, multiply, divide. Think of it as writing a program to help some 3rd graders verify their math skills.

The text menu will prompt the user to enter a choice of operation, after which the user will be prompted to enter 2 numbers; the program will then compute and display the result based on the user's chosen operation. The program must continuously prompt the user for a new choice, until a 'quit' option is selected. Options must be integers. You may assume the user will always enter an integer value. You must write a function for each choice (even though they would be very small functions). You may write other functions that you feel are helpful.

For a simple example of a running of such a problem, see here.

Feel free to be as creative as you want in this program's design.

Turning In

Turn in gcdmain.cpp, polar.cpp, menucalc.cpp. and report.txt. Your lab report should explain the final part of the assignment (Part 3), explaining how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

Due Dates

This assignment is due on

• Part 1: Thursday Sept 19, at 11 pm.
• Parts 2, 3 and report: Sunday Sept 22, at 11 pm.

Please review the general instructions for turning in.