In: Computer Science
IN C++
Create a single function. and pass in a integer which is the the
planet number. 1 will be Mercury and 9 will be Pluto, etc. Return
via reference the planet name, planet weight ( or mass ) in kg, and
the distance from the sun in km. Please create a nice looking table
with headings and show the 9 planets.
Here is an example of the first 4 planets in the table. You will need to add in the next 5.
Planet Planet Planet
Name Mass (kg) Distance
to the
Sun (km)
Mercury 3.3e+23 57910
Venus 4.87e+24 108200
Earth 5.98e+24 149600
Mars 6.42e+23 227940
Code:
#include <iostream>
using namespace std;
struct Planet{
string name;
double weight;
int distane;
};
Planet p[9] = {};
Planet& setPlanet(int i)
{ return p[i]; }
int main()
{
Planet solarSystem[9] = {
{"Mercury", 3.3e+23, 57910},
{"Venus", 4.87e+24, 108200},
{"Earth", 5.98e+24, 149600},
{"Mars", 6.42e+23, 227940},
{"Jupiter", 1.9e+27, 778547},
{"Saturn", 5.7e+26 , 1434000},
{"Uranus", 8.7e+25, 2871000},
{"Neptune", 1.1e+26, 4495000},
{"Pluto",1.4e+22 , 5906380}
};
int i;
cout << "Enter a planet number 1 to 9 or any other number to quit: ";
cin >> i;
while((i > 0) && (i < 10))
{
setPlanet(i-1) = solarSystem[i-1];
cout << "Planet Name: " << p[i-1].name;
cout << "\nPlanet Weight in KGs: " << p[i-1].weight;
cout << "\nPlanet distance: " << p[i-1].distane;
cout << "\n\nEnter a planet number 1 to 9 or any other number to quit: ";
cin >> i;
}
return 0;
}
Output: