I am suppose to write a program that calculates speeding tickets for a c++ class. Below is what the assignment is and what I have so far.
Lab 5 (Chapter 3 Material )
When you move toward a stationary light source, the wavelength of the light emitted by the source decreases. In fact, it decreases according to the following formula:
w1 = w0*( 1 - v/c0 )
w0 = wavelength of color while not moving
w1 = wavelength of color while moving toward source
v = speed of movement toward source
c0 = speed of light (669,600,000 mph)
where w0 is the wavelength of the color emitted by the light source ( that is, as seen by a non-moving observer ), w1 is the wavelength of the color of the emitted light as perceived by you while you’re moving toward the source, v is the speed at which you’re moving toward the source ( assume speed is expressed as miles per hour ), and c0 is the speed of light ( assume light travels 669,600,000 miles per hour ). This phenomenon is known as ‘Doppler shift’, in honor of the Austrian mathematician Johan Christian Doppler ( 1803 – 1853 ).
Note that, in the table given for lab 6, the wavelength of green light is shorter than that of red light. This means that as you approach a red light, the color of the light shifts toward the green spectrum.
Now suppose a police officer stops you for running a red light. Clever person that you undoubtedly are, you argue that as you approached the light, the Doppler shift caused the red light to look green to you. Compassionate person that the officer is, he agrees to write you a ticket for speeding, instead of writing one for running the light. In the particular town in which you committed the infraction, the fine for running a red light is $100, and the fine for speeding is $1 for every mile per hour you travel over the speed limit. The speed limit is 25 miles per hour. Write a program that uses the above formula to calculate and display your speeding fine. You’ll need to solve the equation above for v, your speed as you approached the traffic signal. Referring to the table in lab 6, use .533 microns for the wavelength of green light and .674 microns for the wavelength of red light. Display your fine as currency ( dollars and cents ).
This is what I have so far.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y,v,c,l;
// x = wavelength of color while not moving
// y = wavelength of color while moving toward source
// v = speed of movement toward source
// c = speed of light
cout << "How fast were you going while running stoplight? In MPH please";
cin >> v;
y = .533;
x = .674;
c = 669,600,000;
y = x * (1 - v/c);
cout << "What was the speed limit? in MPH please.";
cin >> l
if ( v > l )
cout << "You were speeding!";
else
if (v<=l )
cout << "You don't get a ticket!";
In C++ the data type 'int' is an abbreviation of the word 'integer', and as all good physicists/mathematicians/programmers know an integer is a whole number (E.g: -1, 0, 1, 2, 3).
Therefore the following code will compile, but will not work as you expect:
int y = 0.533;
Basically the value 0.533 will be 'cast' to an integer type and in this case will be rounded down to 0.
What you need to look into is the 'float' and 'double' data types. They allow for numbers with decimal points.
Another thing is that when you are assigning the variable 'c' a value, you are using commas. In C++ this is not necessary and will in fact give you a syntax error, preventing your program from compiling.
One final thing is that all standard data types have a maximum value. I have a sneaking suspicion that the value '669600000' that you are assigning to the variable 'c' exceeds the maximum value that an 'int' can hold in Windows. I am running 64bit Windows so I cannot check the 32bit Windows limits, however you can use the following program to check and see what data type might be better suited:
#include <limits>
#include <iostream>
usingnamespace std;
int main(){
cout << "\n\n**************************************\n";
cout << "** Some C++ Data Type Limits\n";
cout << "**************************************";
cout << "\n\nThe maximum value of an int is = " << INT_MAX;
cout << "\n\nThe maximum value of an unsigned int is = " << UINT_MAX;
cout << "\n\nThe maximum value of a long int is = " << LONG_MAX;
cout << "\n\nThe maximum value of an unsigned long int is = " << ULONG_MAX;
cout << "\n\nThe maximum value of a long long int is = " << LLONG_MAX;
cout << "\n\nThe maximum value of an unsigned long long int is = " << ULLONG_MAX;
cout << "\n\nThe maximum value of a float is = " << FLT_MAX;
cout << "\n\nThe maximum value of a double is = " << DBL_MAX;
cout << "\n\nThe maximum value of a long double is = " << LDBL_MAX;
cin.get();
}