Built-in functions (Part 3)

Example: painting rooms

A can of paint and a paint brush.

Image: scanrail/iStock/Thinkstock

Let's use the ideas we've learned in a small example. Suppose you have the task of painting the walls (but not the floors or ceilings) of 20 identical rooms, each 6 feet wide, 8 feet long, and 10 feet tall. Assume that 5-gallon cans are available and that a gallon covers 350 square feet. You might wish to figure out the answers to the following two questions:

  • What is the total area to paint, in square feet?
  • What is the number of cans needed?

Glimpse of the future

Later we might think about how to make this more general, such as allowing for

  • different numbers of rooms,
  • different sizes (dimensions) of rooms,
  • cans of different sizes (volume), or
  • different coverage (square feet) per gallon.

Total area

To figure out the total area of the walls of 20 rooms, each 6 feet wide, 8 feet long, and 10 feet tall, we can find the area of one room and multiply that by 20:

The area of all rooms = 20 × The area of one room

Since each room has four walls, of two different sizes, we can break that task into the tasks of figuring out the area of a big wall and the area of a small wall.

The area of one room = 2 × ( The area of big wall + The area of small wall )

The big wall will be the length times the height:

The area of the big wall = 8 × 10

The small wall will be the width times the height:

The area of the small wall = 6 × 10

Putting these together, we get the expression 20 * (2 * ((8 * 10) + (6 * 10))).


Number of cans

5-gallon paint cans for 20 rooms, each 6 × 8 with 10-foot ceilings, 350 square foot per gallon.

To figure out the number of cans we need, we divide the total area by the number of square feet that one can covers:

The number of cans needed = Total area ÷ One can coverage, where

Total area is the total wall areas of the 20 rooms, which we found previously.

Since one gallon covers 350 square feet, a five-gallon can will cover five times that amount:

One can coverage = 5 × 350

So we complete our calculation: (20 * (2 * ((8 * 10) + (6 * 10)))) / (5 * 350)
= 5600 / 1750
= 3.2 .

But we do not want to go to the store and ask for 3.2 cans of paint. We really want an integer.


Fixing the error

The operation of the ceiling function: all the numbers 2.5, 2.0001, and 2.9 are pushed to 3; 1.2 and 1.6 are pushed to 2; 0.8 0.02 are pushed to 1. The integer numbers 0, 1, 2, 3 remain the unchanged.

There are two convenient functions that make numbers into integers. The ceiling function "pushes" a number up to the next integer. The number 3.2 would be pushed up to 4:

ceiling(3.2) gives 4

This is the function to apply to the expression we obtained for the number of paint cans.


The operation of the floor function: all the numbers 2.5, 2.0001, 2.9 are pushed to 2; 1.2 and 1.6 are pushed to 1; 0.8 0.02 are pushed to 0. The integer numbers 0, 1, 2, 3 remain the unchanged.

There is also a floor function that pushes a number down to the next integer. The number 3.2 would be pushed down to 3:

floor(3.2) gives 3

Notice that the floor and ceiling of an integer both equal the integer itself. We will consider more examples once we've learned more programming language details.