#include <iostream>
#include <vector>
#include <string>
#include <cmath>
usingnamespace std;
constfloat Pi = 3.14159;
//Variables
int planets = 1;
int planetchoice;
float percentage;
int main()
{
cout << "Name: Nicolas Aguirre" << endl;
cout << "Filename: P3" << endl;
cout << "Program Objective: To create a program to calculate the surface area of a lune with parameters specified by the user" << endl;
do {
cout << planets << endl;
planets++;
} while (planets <= 6);
cout << "1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon." << endl;
cout << "Choose a planet or the moon: ";
cin >> planetchoice;
if (planetchoice <= 6) {
cout << "Please enter percentage of lune to calculate: ";
cin >> percentage;
if (percentage <= 100)
{
vector<float> planetr(6);
planetr[0] = 2440; //Mercury
planetr[1] = 3397; //Mars
planetr[2] = 6052; //Venus
planetr[3] = 60268; //Saturn
planetr[4] = 71492; //Jupiter
planetr[5] = 1738; //Moon
if (planetchoice == 1)
{
cout << 2 * pow(planetr[0], 2) * percentage * .01 << endl; //LEFT OFF DECIMAL PLACE IS WRONG
}
}
}
}
The thing is, my answers are coming out with the decimal in the wrong spot, so something is off with my math. Precedence issue maybe? I'm lost here. Thanks guys!
Alright, I added code tags to my post. I'm trying to find the surface area of a lune, not the whole sphere. Formula should be S = 2 * r2 * θ. Where θ is the percentage of the lune to be calculated (user inputs this).
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
usingnamespace std;
constdouble pi = 3.14159
int main()
{
vector<double> planet_radius;
planet_radius.push_back(2440); // Mercury
planet_radius.push_back(3397); // Mars
planet_radius.push_back(6052); // Venus
planet_radius.push_back(60268); // Saturn
planet_radius.push_back(71492); // Jupiter
planet_radius.push_back(1738); // Moon
cout << "Name: Nicolas Aguirre" << endl;
cout << "Filename: P3" << endl;
cout
<< "Program Objective: To create a program to calculate the surface\n"
<< "area of a lune with parameters specified by the user\n";
int i{0};
do {
cout << planet_radius[i] << endl;
i++;
} while ( i < planet_radius.size() );
cout
<< "1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon\n"
<< "Choose a planet or the moon: ";
int planet_choice;
cin >> planet_choice;
cout << "Please enter percentage of lune to calculate: ";
double percentage;
cin >> percentage;
cout
<< 2 * pow(planet_radius[planet_choice - 1], 2) * percentage/100 * 2 * pi << '\n';
}
Name: Nicolas Aguirre
Filename: P3
Program Objective: To create a program to calculate the surface
area of a lune with parameters specified by the user
2440
3397
6052
60268
71492
1738
1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon
Choose a planet or the moon: 2
Please enter percentage of lune to calculate: 3
692377
Program ended with exit code: 0
Alright, this looks a bit more correct to me. Yeah the whole idea of calculating a lune and the math involved only makes this assignment harder, I'm terrible at math admittedly.... Tomorrow when I get time, I'm going to add some of this code to my project and see what I can do, I'm also new to C++ so that's double challenging. Thanks guys! Appreciate you all! :)
How did you do on your project? I am currently doing this same project. We have to Use vectors to hold planet names and their corresponding radii. And i am not sure how to do that. Yours has the push back?
yes, push_back() more or less treats a vector as if it were a stack, adding a new item onto the highest order index / back end of the container for you. That should do what you need.
@DaisyDoyle,
please post questions like that in the “Lounge” section of this forum. I think you’ll get better directions there.
“Help about coding” sounds a bit vague, doesn’t it?
This is a forum about C++. Have you been suggested to study C++? It looks very far from sociology.
For data analysis maybe Python could just be what you need? I’m not an expert on the topic.
Python is usually considered a good language for a first approach to programming.
@DaisyDoyle is a stealth spammer. Just delete people like that. (Her only other post was a meaningless "thank you" appended to another thread, a common tactic.)