CSCI 103 - Spring 2024 Introduction to Programming

CS102 Spring 2021 Final - Volume

Write a program that compares the volume of 3 shapes chosen from the options:

As a reminder, the equations for the volume of these shapes are:

The user will input a letter: s (for sphere), o (for cone), or y (for cylinder) followed by the pertinent dimensions. For a sphere the user will only enter a radius value after s. However, for cone and cylinder the user will enter a radius value first followed by a value for the height of the cone or cylinder.

The user will enter all the information about the first shape followed by information for the second shape. You may assume the first shape will always be different than the second shape. The program should then output which shape is larger (e.g. sphere is bigger, cone is bigger, or cylinder is bigger) or the word Tie if both volumes are the same.

Example 1

Input (A cylinder with radius 1 and height 0.5 vs. a sphere with radius 1):

y 1 0.5
s 1

Expected output:

sphere is bigger

Eexample 2

Input (A cone with radius 1 and height 4 and a sphere with radius 1):

o 1 4
s 1

Expected output:

Tie

This problem will be shorter and simpler by using functions. Two recommended options are prototyped below. You DO NOT have to use functions and can change/delete them. Use them if you believe it makes your work faster and simpler, or do not use them if you prefer.

// Given the specified shape's input letter/code (e.g. 's', 'o', 'y'), 
// get's the radius and/or height input and returns the volume
double getVolume(char shape);

// Prints 1 of the 3 output messages based on the input argument which is the 
// letter/code of the largest volume shape (e.g. 's', 'o', 'y').
// Does not handle the case of a tie which must be handled outside the function.
void printBiggerShape(char shape);

The skeleton file can be downloaded with the link: volume.cpp.

(You may need to right-click and choose Save As or Save Target As to download the .cpp file or open it in your browser and save the file.).