• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

View

# Exercises - Procedures with Parameters (redirected from Exercises - Procedures)

last edited by 1 year, 10 months ago

New Exercises (Section A)

1. (a) Write a procedure called printName(), which takes a single string parameter and displays it on the screen.
(b) Write a procedure called printAddress(), which takes a single string parameter and displays it on the screen. Is this any different than part (a)?
(c) Write a procedure called printSignature(), which takes TWO string parameters, a name and an address, and displays them on the screen.

2. Write a procedure called writeMin(), which takes three integer parameters and outputs the lowest of the three values to the screen.
3. Write a procedure called writeArea(), which takes two real (decimal) parameters, length and width, of a rectangle.  The procedure should calculate the area and output the result to the screen.
(a) Extend this procedure so the area is rounded to the nearest integer and then written to the screen.
4. Use the printBar() procedure (see exercise B.1) to write a procedure printTriangle(), which prints a triangle centered on the page.  The number of rows in the triangle, and the character used to print the triangle, are the parameters of the procedure.

For each procedure, you must also write a program that allows you to test the procedure.

Old Exercises (Section B)

1. Write a procedure called printBar() which takes two parameters: the first is a character and the second is an integer from 0 to 80. The function should print out a row of the given character, repeated the number of times given by the integer.  (Turing Solution)

For example, if the user input the character \$ and the number 25, the output would be \$ printed 25 times:

Parameters> \$  25

Output> \$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

2. (challenging, multiple procedures will help) Write a procedure called rectangle() which takes four parameters:  an integer length, an integer width, the x coordinate of the top left corner (column) and its y coordinate (row). The procedure should outline the rectangle in the correct position , with the given length and width using '*' for the border.

3. Write a procedure called validateUser() that will ask for a username and password.  The user has 3 tries to get it right, after which an error message will be output to the screen. (Turing Solution)

4. Write a procedure printDate() that will have inputs of month, day, and year as integer values.  The procedure will output the name of the month, and the numbers for the day and year.
Extend: Check for valid input, and output an error message if any values are out of range

You should review the "if-elsif" conditional statements for this program.  You might also look into the "case" statement, but we will cover this command at a later date.

1. Write and test computeTriangle(), a procedure that returns (by changing input parameters) the area and the perimeter of a triangle given side lengths a, b, and c.  You may wish to use existing triangle functions as part of your procedure.

1. {Note: Cannot be done in Java} A common and useful procedure provides the ability to swap two values.  This concept is used frequently when sorting values.  Write a procedure called swap() that will have two real values as inputs, and the procedure will then exchange the inputs.  This means you will be changing the input parameters, which requires special care when setting up your procedure. (Turing Solution)

2. Modify your program for the Guessing Game.  Replace some blocks of code with one or more procedures to make the overall program simpler to read and understand.

3.  Write a program which makes change for an amount of money less than a dollar. The program should have three functions or procedures called quarters(), dimes() and nickels().   The main program should call each subprogram in turn and then print out the numbers of each type of coin. (Turing Solution with 3 Procedures)

Sample Output (user input in red)

How much change?  92

Quarters = 3

Dimes = 1

Nickels = 1

Pennies = 2

4. Many advanced calculators have a fraction feature that will simplify fractions for you.  You are to write a program that will accept for input a positive or negative integer as a numerator and a positive integer as a denominator, and output the fraction in simplest form. That is, the fraction cannot be reduced any further, and the numerator will be less than the denominator. You can assume that all input numerators and denominators will produce valid fractions.  Remember the div and mod operators.

Implement this by writing a procedure that takes in a numerator and denominator, and returns an integer, a new reduced numerator and denominator (i.e., it can change the input parameters).  You may also use a combination of functions and procedures for other features in your program.

Sample Output (user input in red)

Numerator:  14

Denominator:  3

Result:  4 2/3

Numerator:  8

Denominator:  4

Result:  2

Numerator:  5

Denominator:  10

Result:  1/2