Create a program ( in c++) will prompt the user to enter a time in days and calc
ID: 3750422 • Letter: C
Question
Create a program ( in c++) will prompt the user to enter a time in days and calculate the magnitude of the binary star for that time. Your program should use a simple if to ensure that the entered time is valid (greater than or equal to 0) and prompt the user to re-enter if the entered time is invalid. Then use an if-elseif structure to determine the magnitude for the entered time. Your output should be similar to “At _____ days the magnitude of the binary star is _______.” Output the days to 3 significant figures and the magnitude to 5 significant figures. Hint: The fmod function will calculate the remainder of floating point division
Problem: Astronomers have measured the magnitude (brightness) of a binary star as a function of time and found it to have periodicity (cycle) of 6.4 days. The magnitude varies as given below.
from times t = 0 days until t = 0.9 days the magnitude is 2.5
from times t = 0.9 days until t = 2.3 days the magnitude may be calculated by the formula
3.355 - ln(1.352 + cos(*(t - 0.9)/0.7))
from times t = 2.3 days until t = 4.4 days the magnitude is 2.5
from times = 4.4days until t = 5.2 days the magnitude may be calculated by the formula 3.598 - ln(1.998 + cos( *( t - 4.4)/0.4))
from times = 5.2 days until t = 6.4 days the magnitude is 2.5 At 6.4 days the cycle begins again as if the time was 0 days.
A graph for 2 cycles (12.8) days is given below.
Can somone please help me with this program?
4.4 4.2 3.8 3.6 3.4 3.2 2.8 2.6 2.4 10 12 14Explanation / Answer
//Header files
#include<iostream>
#include<math.h>
using namespace std;
//Define PI
#define PI 3.14
//main() starts
int main()
{
double time, magnitude;
//Input time
cout<<"Enter a time in days: ";
cin>>time;
//If less than 0, then display message
if(time < 0) //Statement 1
cout<<"Wrong input. Try again."; //Statement 2
//In the question, it is said that the user shoud re-renter the time if less than 0
//But it is also stated that we should use only a simple if statement which is down above.
//If it is needed that the user should re-enter the value, then we must use a loop.
//In that case, use the following code in place of Statement 1 and 2 and remove Statement 3, 4 and 5.
//while(time < 0)
//{
// cout<<"Wrong input. Try again: ";
// cin>>time;
//}
//Otherwise proceed
else //Statement 3
{ //Statement 4
//Take e temporary variable and save the value of (time % 6.4) in it
float temp = fmod(time, 6.4);
//Continue as per the conditions
if(temp < 0.9)
magnitude = 2.5;
else if(temp < 2.3)
magnitude = 3.355 - log(1.352 + cos(PI * (temp - 0.9) / 0.7));
else if(temp < 4.4)
magnitude = 2.5;
else if(temp < 5.2)
magnitude = 3.598 - log(1.998 + cos(PI * (temp - 4.4) / 0.4));
else if(temp < 6.4)
magnitude = 2.5;
//Set output stream to display the trailing zeroes after decimal
cout.setf(ios::showpoint);
//Set the precision for days
cout.precision(3);
cout<<"At "<<time<<" days the magnitude of the binary star is ";
//Set the precision for magnitude
cout.precision(5);
cout<<magnitude<<". ";
} //Statement 5
return 0;
}
Outputs are also provided for some sample test cases. User input is in bold.
Test Case 1:
Enter a time in days: 0.5
At 0.500 days the magnitude of the binary star is 2.5000.
Test Case 2:
Enter a time in days: 1.6
At 1.60 days the magnitude of the binary star is 4.3991.
Test Case 3:
Enter a time in days: 11.2
At 11.2 days the magnitude of the binary star is 3.6000.
Test Case 4:
Enter a time in days: 4.96
At 4.96 days the magnitude of the binary star is 3.0751.
Kindly give a thumbs up, if found useful. Comment for queries. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.