CIS 111 Functions

Objectives

  • Create functions to modularize code and eliminate duplicated code
  • Prompt the user for input
  • Get input from the user
  • Convert user input into the proper data type
  • Perform simple calculations
  • Display output to the user
  • Create flowcharts containing I/O, repetition, and functions

Overview

For the project listed below, write a flowchart and a Python program to solve the problem. The source code for this program, and all others due in this course, must include comments at the beginning of the source code which contain your name, the course, the date, the assignment number, and a short description. The source code must also follow standard formatting conventions as discussed in class.

It is considered cheating to copy work. Problems may be discussed, but not copied.

Program requirements

(40 pts) Write a flowchart and Python program that asks the user for a width and height of a rectangle. The program will then display the perimeter, the area, and display the rectangle using asterisks. The main program will call functions to do most of its work. The required functions are:

  • calcPerimeter: It should accept an integer height and width and return the twice the sum of the height plus the width.
  • calcArea: It should accept an integer height and width and return the height multiplied by the width.
  • displayRectangle: It should accept an integer height and width and display a rectangle matching those dimensions on the screen using the '*' character.
  • getInt: It should accept a string prompt and integer minimum and maximum values. It shold display the prompt and get an integer from the user. If the input is out of bounds, then an error message should be displayed and input should be requested again. See the sample runs for details.

Once the functions are working, have the main program call them to get the width, get the height, and then display the perimeter, the area, and a representation of the rectangle itself.

Hierarchy chart

hierarchy chart for this assignment

Formulas

  • perimeter = 2 * (width + height)
  • area = width * height

Sample runs

Three sample runs so you can check your work:

Enter width (1 - 60): 6 Enter height (1 - 20): 3 The perimeter is 18 The area is 18 ****** ****** ****** Enter width (1 - 60): 9 Enter height (1 - 20): 2 The perimeter is 22 The area is 18 ********* ********* Enter width (1 - 60): -4 Integer value must be between 1 and 60, please re-enter: 70 Integer value must be between 1 and 60, please re-enter: 4 Enter height (1 - 20): 1 The perimeter is 10 The area is 4 ****

Rubric

  • 10 points for a correct and professional flowchart
  • 5 points for proper documentation comments and style (proper and consistent indentation, variable names start with lowercase letter, etc.)
  • 3 points for a properly working calcPerimeter function
  • 3 points for a properly working calcArea function
  • 5 points for a properly working displayRectangle function
  • 7 points for a properly working getInt function
  • 7 points for having the main program properly call the functions and use their returned values (if any)