Weight on other planets

Problem

The solution below reflects the result of all four parts

Part 1

  • Write a program that lists the planets
  • Start with Earth and then go in order, closest to furthest from the Sun
  • Use an array and companion variable to keep track of the array_size

Part 2

  • Some people have strong feelings about Pluto being a planet
  • Adapt your program that lists the planets to first ask the user if they think Pluto is a planet

Part 3

  • Next, adapt your program to prompt the user for a weight (in lbs)
  • Print back a table showing the weight on each of the planets using weight ratios of
    • 1.0, .38, .91, .38, 2.34, 1.06, .92, 1.19, .06 for
    • “Earth”, “Mercury”, “Venus”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”, “Sun”, “Moon” respectively
  • Use a parallel array (NOTE: classes are a better way to represent paired data which we learn about in chapter 9.)

Part 4

  • Add the Sun and Moon (to end of the array) using weight ratios of
    • 27.072, .166 for
    • “Sun”, “Moon” respectively

Solution

#include <iostream>
#include <string>
 
using namespace std;
 
/*
	Function to remove planet from list
	@param planet_name planet to remove
	@param planets list of planets
	@param weights weight ratios for planets
	@param array_size
	@return new array size
*/
int remove_planet(string planet_name, string planets[], double weights[], int array_size)
{
	// loop over each element of the array
	for (int i = 0; i < array_size; i++)
	{
		// looking for the requested planet name to remove
		if (planets[i] == planet_name)
		{
			// remove this element
			planets[i] = planets[array_size - 1];
			weights[i] = weights[array_size - 1];
			array_size--;
		}
	}
 
	// return the new size
	return array_size;
}
 
int main()
{
	// Goal: print a list of planets starting with Earth and then Mercury - Pluto
 
	// These are not constants because we're going to modify them during the program
	string planets[] = { "Earth", "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto", "Sun", "Moon"};
	double weight_ratios[] = { 1.0, .38, .91, .38, 2.34, 1.06, .92, 1.19, .06, 27.072, .166 };
	int array_size = 11;
 
	// prompt user if pluto is planet
	cout << "Is Pluto a planet (Y/N)? ";
	string input;
	cin >> input;
 
	// if it is not, remove it from the list
	if (input == "N")
	{
		// We need to update the size of the array because the function can't modify it
		int new_array_size = remove_planet("Pluto", planets, weight_ratios, array_size);
		array_size = new_array_size;
	}
 
	// prompt user for weight
	cout << "Weight: ";
	double user_weight;
	cin >> user_weight;
 
	cout << "PLANET\tWEIGHT" << endl;
	for (int i = 0; i < array_size; i++)
	{
		// Calculate and print out the weight for the ith planet
		double weight_on_planet_i = user_weight * weight_ratios[i];
		cout << planets[i] << "\t" << weight_on_planet_i << endl;
	}
	cout << endl;
 
	system("pause");
	return 0;
}
cs-142/weight-on-other-planets.txt · Last modified: 2015/05/25 18:22 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