28.9k views
3 votes
Write a function, maxRadius, to find the index of the planet with the largest radius in the array. The function should: Be named maxR

1 Answer

3 votes

Answer:

The following c code has the maxR function as the calculator of planet radius.

Code:

#include<iostream>

#include<string>

using namespace std;

//if is not asked to import a math library, it can be used the following Pi value.

const double PI = 3.14;

class Planet

{

private:

string planet;

double r;

public:

Planet ()

{

this->planet = "";

this->r = 0.0;

}

Planet (string name, double r)

{

this->planet = name;

this->r = r;

}

string collectName () const

{

return this->planet;

}

double collectR () const

{

return this->r;

}

double collectVolume () const

{

return 4 * r * r * r * PI / 3;

}

};

int

maxR (Planet * planets, int s)

{

double maxR = 0;

int index_of_max_r = -1;

for (int index = 0; index < s; index++)

{

if (planets[index].collectR () > maxR)

{

maxR = planets[index].collectR ();

index_of_max_r = index;

}

}

return index_of_max_r;

}

int

main ()

{

Planet planets[5];

planets[0] = Planet ("On A Cob Planet", 1234);

planets[1] = Planet ("Bird World", 4321);

int idx = maxR (planets, 2);

cout << planets[idx].collectName () << endl;

cout << planets[idx].collectR () << endl;

cout << planets[idx].collectVolume () << endl;

}

User C Blanchard
by
5.1k points