Complete the Program

Complete the Program

This program uses 2 box_area functions. One of them takes 1 parameter, where it is assumed that all sides are the same length. The other takes 3 parameters, height, width, and depth.

Evaluate the current code and add the functions where indicated by the comments.

Decide for yourself which datatype to use, based on the provided code.

#include <iostream>

// Add your functions here

int main()
{
	float box_area1 = box_area(15.f);
	float box_area2 = box_area(10.f, 15.f, 20.f);

	if (box_area1 == box_area2)
		std::cout << "Box area 1 is the same size as box area 2.\n";
	else if (box_area1 > box_area2)
		std::cout << "Box area 1 is " << box_area1 - box_area2 << "units larger than box area 2.\n";
	else
		std::cout << "Box area 2 is " << box_area2 - box_area1 << "units larger than box area 1.\n";
}

Answer

#include <iostream>

// NEW FUNCTIONS --------------------------------------
float box_area(float side) {
    return side * side*side;
}

float box_area(float length, float width, float height) {
    return length * width * height;
}
// ----------------------------------------------------

int main()
{
	float box_area1 = box_area(15.f);
	float box_area2 = box_area(10.f, 15.f, 20.f);

	if (box_area1 == box_area2)
		std::cout << "Box area 1 is the same size as box area 2.\n";
	else if (box_area1 > box_area2)
		std::cout << "Box area 1 is " << box_area1 - box_area2 << "units larger than box area 2.\n";
	else
		std::cout << "Box area 2 is " << box_area2 - box_area1 << "units larger than box area 1.\n";
}

Floats are universally used for storing the area, as well as passed as all arguments, so it would make sense for the functions to use float as both parameter and return types.

Questions