Question

In: Computer Science

IN C++ Create a single function. and pass in a integer which is the the planet...

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

Solutions

Expert Solution

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:


Related Solutions

C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Create an integer to std_logic_vector function(i2std)
Create an integer to std_logic_vector function(i2std)
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Array & Function - Can I pass a single parameter to a function that takes two...
Array & Function - Can I pass a single parameter to a function that takes two arguments: data passed from: arrayOne(4); to function: string function (array[], int)
. Create a Python function that asks the user for a number (integer). The function should...
. Create a Python function that asks the user for a number (integer). The function should then tell the user how many hundreds can go into the number, and how much is left over. Hint: the % operator calculates the remainder of a division. For example, 10 % 3 gives a result 1. Hint2: Deal with the positive and negative values in separate parts of an if-else structure. Get the calculation work for positive values first. For negative values, make...
Given the following C function prototype which accepts an integer argument, complete the implementation of the...
Given the following C function prototype which accepts an integer argument, complete the implementation of the function in C language to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. USE the following function int countOnes(int number) Write a full c program that has the header required and...
- Create a list with 40 integer random numbers - With a function (def) create two...
- Create a list with 40 integer random numbers - With a function (def) create two new lists from the list created by random numbers, in which on one are the even elements and on the other the odd elements - Create two variables with the length of both new lists and print the variables All exercices must be done in Phyton
IN C++: Create sets of 1 Million integer, where no integer is repeated. Insert these numbers...
IN C++: Create sets of 1 Million integer, where no integer is repeated. Insert these numbers to an AVL tree and an R-B tree. Using a random number generator, select 10% of the numbers in the trees and delete them. Repeat the experiment 10 times. Report your answers in a tables
In C language: Write a function which can receive a float array, an integer number(number of...
In C language: Write a function which can receive a float array, an integer number(number of elements) as input arguments and print all the elements in the array with 2 decimal precision.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT