For my intro to c++ class I have this assignment due. I've already completed the first part (the factorial function). But I'm not sure how to create, or where to put, the other functions in my code.
You are a Scientist at NASA studying the relationship between a newly discovered planet (code named Omega) and its nearby asteroids. Towards this goal your boss has asked you to create a written report on one of the asteroids. This report requires a number to be used as an identifier for the asteroid and a measurement of the estimated gravitational force between Omega and the asteroid of your choice.
--------------------------------------------
First you have decided to use the factorial of a number supplied by the user as the asteroid’s ID.
Part 1: Factorial Function:
Requirements:
-Write a function that takes an integer as its parameter.
-The function must return the factorial of this integer.
-This function must be able to calculate the factorial of any integer greater than or equal to zero.
*As stated in lab. Cin and getline cannot be used to receive user input inside this function. It must be passed in through a parameter.
Example:
Given: 5
Return: 120
As 120 is 5*4*3*2*1
-------------------------------------------
Next you need to make the function that can be used to calculate the gravitational force between planet Omega and the asteroid.
Part 2: Gravitational Force:
Formula for Gravitational Force:
F is the force between the masses,
G is the gravitational constant (6.673 x 10-11 N m2/kg2) ,
m1 is the first mass,
m2 is the second mass, and
r is the distance between the centers of the masses.
For this problem you must create a function that calculates the gravitational force between planet Omega and a nearby passing Asteroid. The mass of planet Omega is: 3.2 × 1010 kg. The mass of the asteroid being 2.43 × 105 kg.
Requirements:
-The function must take 1 parameter, the distance between Omega and the Asteroid.
-The function must return the force between Omega and the Asteroid as a float.
*As stated in lab. Cin and getline cannot be used to receive user input inside this function. It must be passed in through a parameter.
Example:
Given: 350
Return: 4.236
As G * ((3.2 × 1010) * (2.43 × 105)) / (3502)=4.236
Part 3: Main:
Your main is the driver that must call the previous two functions then write the results as a report to a txt file. It must allow the user to enter in an integer, then find and store the factorial of that integer to be used as the asteroid’s id. The driver must then allow the user to enter the distance between Omega and the Asteroid, then find and store the force of gravity between Omega and the Asteroid. Finally the driver must write this information out to a file named report.txt In a format similar to this:
--------------------------------
Asteroid - [ID]
Is currently within [distance] kilometers of planet Omega.
At this distance the two have a gravitational force of [Force] Newtons between them.
-------------------------------
Where ID is the ID generated by the factorial function.
distance is the distance between Omega and the asteroid entered by the user.
Force is the force of gravity between the two bodies as calculated by the gravitation force function.
Aside from writing these three things to the file in a neat manner, the rest of the content in the report can be anything.
Requirements:
-Ask the user for an integer to be used to generate an id with the factorial function.
-Save the id generated from the factorial function when it is given the integer supplied by the user.
-Ask the user for the distance between Omega and the asteroid
-Save the force between Omega and the Asteroid as generated by gravitational force function.
-Write a report to a file named report.txt using ofstream.
-Have the report include at least the ID for the asteroid, the distance between the two bodies, and the gravitational force between them.
-Input Validation
*Do not allow the user to enter a number less than 0. If they attempt to enter a negative number, have them re-enter a number until it is positive.
Full Example Run:
“What is your name?”
-Dr. Scientist
“Enter a number to be used to generate an Asteroid ID”
-5
“The asteroid ID is 120”
“Enter the distance between Omega and Asteroid 120”
-350
“The force of gravity between them is 4.236 Newtons”
Sample report.txt:
--------------------------------
Asteroid - 120
Is currently within 350 (I am aware of how impossible this is) kilometers of planet Omega.
At this distance the two have a gravitational force of 4.236 Newtons between them.
It is the opinion of this committee that the asteroid here after be known as “Buttercup”
Signed,
Dr Scientist.
This is my code so far:
#include <iostream>
using namespace std;
int calculateFactorial (int theNumber); //function prototype
int main () // start of function body
{
int number; //initialize variable
//cout statement asking user for an integer greater than or equal to 0
//cin statement for user to input the integer
cout << "Please enter an integer greater than or equal to 0: ";
cin >> number;
//while statement for input validation
while(number < 0)
{
cout << "You must enter an integer greater than or equal to 0. Try again: " << endl;
cin >> number;
}
//cout statement displaying the factorial of the number that the user enters
cout << "The factorial of "<< number <<" is: "<< factorial <<endl;
return 0; //end function body
}
int calculateFactorial(int theNumber) //re-state function prototype
{
//initializing variables for function that will calculate factorial
int count;
int value = 1;
for (count = 1; count <= theNumber; count++) //for loop calculates factorial
value *= count;
return value; //the actual factorial that will displayed in above cout statement
}
Thank you so much, if anyone can help it would be greatly appreciated!!
#include <iostream>
#include <fstream>
#include <string>
int factorial(int n)
{
if (n<2) return 1;
return n * factorial(n-1);
}
float GravForce(float dist)
{
constdouble G = 6.673e-11; // Gravitational constant
constdouble m1 = 3.2e10; // mass of Omega
constdouble m2 = 2.43e5; // mass of asteroid
return G * m1 * m2 / (dist*dist);
}
int main()
{
std::string name;
std::cout << "What is your name? ";
std::getline(std::cin,name);
int AsteroidSeed;
std::cout << "Enter a number to be used to generate an Asteriod ID: ";
std::cin >> AsteroidSeed;
while (AsteroidSeed < 1)
{
std::cout << "Invalid input, try again: ";
std::cin >> AsteroidSeed;
}
int AsteroidID = factorial(AsteroidSeed);
std::cout << "The Asteroid ID is " << AsteroidID << std::endl;
float Distance;
std::cout << "Enter the distance between Omega and Asteroid " << AsteroidID << ": ";
std::cin >> Distance;
while (Distance < 0.0f)
{
std::cout << "Invalid input, try again: ";
std::cin >> Distance;
}
float GForce = GravForce(Distance);
std::cout << "The force of gravity between them is " << GForce << " Newtons" << std::endl;
std::ofstream fout("report.txt");
fout << "--------------------------------" << std::endl
<< "Asteroid - " << AsteroidID << std::endl
<< "Is currently within " << Distance << " (I am aware of how impossible this is) kilometers of planet Omega." << std::endl
<< "At this distance the two have a gravitational force of " << GForce << " Newtons between them." << std::endl
<< "It is the opinion of this committee that the asteroid here after be known as \"Buttercup\"" << std::endl
<< "Signed," << std::endl
<< name << '.';
return 0;
}
Console:
What is your name? Dr. Scientist
Enter a number to be used to generate an Asteriod ID: 5
The Asteroid ID is 120
Enter the distance between Omega and Asteroid 120: 350
The force of gravity between them is 4.23586 Newtons
Report:
--------------------------------
Asteroid - 120
Is currently within 350 (I am aware of how impossible this is) kilometers of planet Omega.
At this distance the two have a gravitational force of 4.23586 Newtons between them.
It is the opinion of this committee that the asteroid here after be known as "Buttercup"
Signed,
Dr. Scientist.