Do you wanna build a snowman?

Problem

  • You want to play in the snow in May. So you build a machine to create snow. You want a program to calculate how much snow you’ll need in order to build 1 of 3 choices:
    • A bunch of snowballs (user gives width and count)
    • A snowman (user gives 3 widths)
    • An ideal snowman (user gives total height)

Solution

/*
Test case 1:
Input: opt 1, asdf, 1, 2  (invalid snowball count, snowball opt)
Expected Output: reprompt, 4.18879 cub in
Actual Output: reprompt, 4.18879 cub in
 
Test case 2:
Input: opt 2, 6, 4, 2 (snowman of user specified heights)
Expected Output: 150.79632 cub in
 
Test case 3:
Input: opt 3, 12 (snowman of ideal proportions)
Expected Output: 150.79632 cub in
*/
 
#include <iostream>
#include <string>
 
using namespace std;
 
const double PI = 3.14159265359;
 
// These must be declared before the function that calls them (e.g., main), but can be defined below
void print_menu();
int get_user_input(int min, int max);
double calculate_snowball_volume(double width);
double calculate_snowman_volume(double width1, double width2, double width3);
 
int main()
{
	double total_volume = 0.0;
 
	// prompt user for choice
	print_menu();
 
	int choice = get_user_input(1, 3);
 
	// if choice is 1
	if (choice == 1)
	{
		// prompt for width w and count c of snowballs
		cout << "You chose 1. How many snowballs? ";
		int snowball_count = get_user_input(0, INT_MAX);
 
		cout << "How wide do you want your snowballs? ";
		int snowball_width = get_user_input(0, INT_MAX);
 
		// calculate volume for c snowballs of width w
		total_volume = calculate_snowball_volume(snowball_width);
		total_volume = total_volume * snowball_count;
	}
 
	// if choice is 2
	else if (choice == 2)
	{
		// prompt for 3 widths
		int bottom_width, mid_width, top_width;
		cout << "Give me the bottom width: ";
		bottom_width = get_user_input(0, INT_MAX);
 
		cout << "Give me the middle width: ";
		mid_width = get_user_input(0, INT_MAX);
 
		cout << "Give me the top width: ";
		top_width = get_user_input(0, INT_MAX);
		// calculate volume of snowman with three widths
		total_volume = calculate_snowman_volume(bottom_width, mid_width, top_width);
	}
 
	// if choice is 3
	else if (choice == 3)
	{
		// prompt for height
		cout << "How tall of an ideal snowman do you want? ";
		int height;
		height = get_user_input(0, INT_MAX);
 
		// calculate volume of ideal snow man from height
		total_volume = calculate_snowman_volume(height/2.0, height/3.0, height/6.0);
	}
 
	// Report total volume
	cout << "Total volume of snow needed is " << total_volume << " cubic inches" << endl;
 
	system("pause");
	return 0;
 
}
 
/*
	Just print the menu (doesn't read input)
*/
void print_menu()
{
	cout << "Choose from the following\n"
		<< "1. A bunch of snowballs\n"
		<< "2. A snowman\n"
		<< "3. An ideal snowman\n"
		<< "Choice: ";
 
	return;
}
 
/*
	Reads for integer input and reprompts until valid integer input is provided within range
	@param min minimum value for range of input
	@param max maximum value for range of input
	@return first valid input integer
*/
int get_user_input(int min, int max)
{
	int input;
	// read in from cin
	cin >> input;
 
	// while it's bad (non-int or not in range)
	while (cin.fail() || input < min || input > max)
	{
		// send message, reprompt
		cout << "That's bad input. Gimme a value between " << min << " and " << max << ":";
		if (cin.fail())
		{
			cin.clear();
			string dummy;
			cin >> dummy;
		}
		// read again from cin
		cin >> input;
	}
 
	return input;
}
 
/*
	Calculate the volume of a sphere of particular width
	@param width width of snowball to calculate volume for
	@return volume of snowball with given width
*/
double calculate_snowball_volume(double width)
{
	double volume = 0.0;
 
	// volume is 4/3 pi r^3 (note that r is half of the width)
	volume = 4 / 3.0 * PI * pow(width/2.0, 3);
 
	return volume;
}
 
/*
	Calculate the volume of a three-ball snowman with balls of particular widths
	@param width1 width of bottom snowball to calculate volume for
	@param width2 width of mid snowball to calculate volume for
	@param width3 width of top snowball to calculate volume for
	@return volume of snowball with given width
*/
double calculate_snowman_volume(double width1, double width2, double width3)
{
	double volume = 0.0;
 
	// add volume for three snowballs
	volume += calculate_snowball_volume(width1);
	volume += calculate_snowball_volume(width2);
	volume += calculate_snowball_volume(width3);
 
	return volume;
}
cs-142/do-you-wanna-build-a-snowman.txt · Last modified: 2015/05/19 18:01 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0