Computer Science and Programming
Prof. Gregory Safko

Lab 4

Assigned: February 17, 2003

Due February 24, 2003

 


1. Vowels (vowels.cpp)

Write a function isVowel that accepts an argument of type char and returns a bool value. (What will the prototype and function header look like?) The function should return true if the character is a vowel ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', or 'u'), false otherwise.

(Hint: the #include file <cctype> contains some useful functions, in particular toupper and tolower, which accept a char argument and return the character converted to uppercase or lowercase. Letters which are already in the correct case, and nonletters, are returned unchanged. For example, toupper('a') returns 'A', toupper('B') returns 'B', tolower('C') returns 'c', and tolower('4') returns '4'. This can help you make only 5 comparisons instead of 10.)

Also write a suitable main program to demonstrate your function.

The prototype should look like this: 

    bool isVowel(char v);

 


2. Triangles (tri1.cpp)

Write a function makeTriangle that takes a single int parameter t and draws a triangle that is t rows by t columns.

Here are two example runs (User input is underlined here for illustration only.)

Please enter a nonnegative integer: 4
*
**
***
****
Press any key to continue


Please enter a nonnegative integer: 1
*
Press any key to continue

The prototype should look like this: 

    void makeTriangle(int);


3. More Triangles (tri2.cpp)

Modify the function makeTriangle (from above) that takes a single int parameter t and draws a triangle that is t rows by t columns, and then finishes with it's mirror image below it

Here are two example runs (User input is underlined here for illustration only.)

Please enter a nonnegative integer: 4
*
**
***
****

***

**

*
Press any key to continue

 


Please enter a nonnegative integer: 1
*
Press any key to continue


Please enter a nonnegative integer: 2
*

**

*
Press any key to continue

 

The prototype should still look like this: 

    void makeTriangle(int);


4. Compound Interest (interest.cpp)

Write a program that calculates the interest earned after a given period of time. The user will be prompted for an initial investment, the interest rate, and the length in years that the investment will be annualizing.

The calculation for compound interest is:

    a = p * (1 + r)

Where p = the original amount invested (i.e. the principal)

            r = the annual interest rate

            n = the number of years

            a = the amount on deposit at the end of the nth year

A sample run is as follows:  

Please enter an initial investment: 1000

Please enter the interest rate: 5

Please enter the number of years: 10

 

Year        Amount on Deposit

1           1050.00 

2           1102.50

3           1157.62

4           1215.51

5           1276.28

6           1340.10

7           1407.10

8           1477.46

9           1551.33

10          1628.89

 

Press any key to continue

 

Some ideas and hints:

1. To calculate a number raised to another power, use the pow( ) function, by putting #include <cmath> in your preprocessor directive area. The function pow(double base, double exponent) returns a value of type double. Therefore, to calculate 4.1 raised to the power of 3, (i.e., 4.13), you would use pow(4.1,3); and use the value returned from that function.

2. The annual interest rate r should be entered by the user as a number, but when plugged into the equation it should be converted to "percentage as a decimal" form. In other words, if the user enters 5, you plug .05 (or 5/100) into the equation.

3. To print the 0's in the tenths and hundredths positions, use the technique described by Savitch on page 56:

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);

 

Back to the main course page