Introduction: C++: Calculate the Velocity of Star/Galaxy/Quasar Using Relativistic Redshift

About: Occupation: tech support

C++ Program that calculates the velocity of a star/galaxy/quasar using relativistic redshift. Below and attached. PDF flowchart also attached.

#include 
#include

using namespace std;

int main()

{ // start main function

double observedWavelength;
double elementWavelength;
double changeWavelength;
double redshift;
double velocity;
double topOfFraction;
double bottomOfFraction;

cout << "Program to calculate the velocity of a star, galaxy or quasar using relativistic redshift.\n";

cout << "Enter the observed wavelength in Angstroms: ";
cin >> observedWavelength;

cout << "Enter the element's wavelength in Angstroms: ";
cin >> elementWavelength;

// Calculate the change in the wavelength
changeWavelength = observedWavelength - elementWavelength;
cout << "The change in wavelength is: " << changeWavelength << endl;

// Calculate the redshift value
cout << "The redshift formula is: redshift = (change in wavelength/wavelength of the element.)\n";
redshift = (changeWavelength/elementWavelength);
cout << "The redshift of the star or galaxy is: " << redshift << endl;
cout << "=============\n";

// Calculate the velocity of the star, galaxy or quasar
cout << "The relativistic redshift formula is: ( (((redshift+1)^2) - 1)/(((redshift+1)^2) + 1) ) * 300000)\n";

// Raising a number to a power function: pow(a, b) = a to the b power
// Calculating the fraction
topOfFraction = ((pow((redshift+1),2))-1);
cout << "The top of the fraction is: " << topOfFraction << endl;
bottomOfFraction = ((pow((redshift+1),2))+1);
cout << "The bottom of the fraction is: " << bottomOfFraction << endl;

// Calculate velocity using top and bottom of fraction
velocity = ((topOfFraction)/(bottomOfFraction)) * 300000;
cout << "The velocity of the star, galaxy or quasar is: " << velocity << " km/s.\n";
cout << "Note that a positive value means the object is moving away from Earth.\n";
cout << "A negative value means that the object is moving towards Earth.\n";
cout << "Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.\n";

return 0;

} // end main function

Supplies

  1. Code::Blocks Integrated Development Environment (or any other C++ IDE)
  2. C++ compiler (Code::Blocks includes the GCC)

Step 1: C++ Code

C++ Program that calculates the velocity of a star/galaxy/quasar using relativistic redshift. Below and attached. Flowchart is also attached.

#include 
#include

using namespace std;

int main()

{ // start main function

double observedWavelength;
double elementWavelength;
double changeWavelength;
double redshift;
double velocity;
double topOfFraction;
double bottomOfFraction;

cout << "Program to calculate the velocity of a star, galaxy or quasar using relativistic redshift.\n";

cout << "Enter the observed wavelength in Angstroms: ";
cin >> observedWavelength;

cout << "Enter the element's wavelength in Angstroms: ";
cin >> elementWavelength;

// Calculate the change in the wavelength
changeWavelength = observedWavelength - elementWavelength;
cout << "The change in wavelength is: " << changeWavelength << endl;

// Calculate the redshift value
cout << "The redshift formula is: redshift = (change in wavelength/wavelength of the element.)\n";
redshift = (changeWavelength/elementWavelength);
cout << "The redshift of the star or galaxy is: " << redshift << endl;
cout << "=============\n";

// Calculate the velocity of the star, galaxy or quasar
cout << "The relativistic redshift formula is: ( (((redshift+1)^2) - 1)/(((redshift+1)^2) + 1) ) * 300000)\n";

// Raising a number to a power function: pow(a, b) = a to the b power
// Calculating the fraction
topOfFraction = ((pow((redshift+1),2))-1);
cout << "The top of the fraction is: " << topOfFraction << endl;
bottomOfFraction = ((pow((redshift+1),2))+1);
cout << "The bottom of the fraction is: " << bottomOfFraction << endl;

// Calculate velocity using top and bottom of fraction
velocity = ((topOfFraction)/(bottomOfFraction)) * 300000;
cout << "The velocity of the star, galaxy or quasar is: " << velocity << " km/s.\n";
cout << "Note that a positive value means the object is moving away from Earth.\n";
cout << "A negative value means that the object is moving towards Earth.\n";
cout << "Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.\n";

return 0;

} // end main function