Again for the Doppler shift

Hello guys!
I treed to solve the problem coding for Doppler shift, and look what I have done:
#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
double x;
double y;
double v;
double c;
float i;
x = x+i;
y = y+i;
v = v+i;
// x = wavelength (sound or lıght) when object is static.
// y = wavelength (sound or lıght) when object is moving.
// v = source speed
// c = wave speed
// l = limit
cout << " Wavelenght of static object is...";
cin >> x;
cout << " Wavelenght of moving object is ...";
cin >> y;
cout << " The object speed is...";
v = (1-(c*y)/x);
cin >> v;
y = y+i;
c = 300000000;

cout << "The object speed ıs ...";
system ("pause");
return 1;
}

I coded it by Dev-C++, and when I execute it Pass by console, insert the wavelength value for static object, insert wavelength value for moving object,
but I haven't calculation and program give me the answer without any value.

I noted c = 300000000 for light speed, how can choose between sound speed (340 m/s) and light speed ?

Hope to understand me.
Thank You


Here are some comments regarding what is wrong with your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdlib> //You don't need this
#include <iostream>
using namespace std;

int main ()
{
	double x; //You can use 'double x, y, v, c;' instead of these 4 lines.
	double y;
	double v;
	double c;
	float i;
	x = x+i; // You haven't set x or i yet.  This does nothing
	y = y+i; // You haven't set y or i yet.  This does nothing
	v = v+i; // You haven't set v or i yet.  This does nothing

	cout << " Wavelenght of static object is...";
	cin >> x;
	cout << " Wavelenght of moving object is ...";
	cin >> y;
	cout << " The object speed is...";
	v = (1-(c*y)/x); //You haven't set c yet.
	cin >> v; //You just overwrote your calculation
	y = y+i; //What is i?  It hasn't been set
	c = 300000000; //You're finally setting c, but you don't use it after this point.  Do this earlier.

	cout << "The object speed ıs ..."; //Do you expect to see anything after this?
	//cout << v; //Put this here.

	system ("pause");	
	return 1; //return 0 is standard, 1 ussually means an error.
}


Regarding c, use an 'if' statement to determine the value:
1
2
if ("light speed is required") c = 300000000;
else c = 340;


Replace the condition in the if statement with something meaningful.
I see you've been stuck on this for a while:
http://cplusplus.com/forum/general/58865/

Since this means it's not homework here's your solution. Study what I did here. You need to ensure that EVERY variable gets set before you use it and follow the flow of your program from start to finish as order matters.

From the first post:
AldaBIG wrote:
w1 = w0*( 1 - v/c0 ) is the formulae.
w0 = wavelength of stable source (sound or light)
w1 = wavelength (of sound or light while moving toward source)
v = speed of movement toward source
c0 = speed of light 300000000 m/s or sound's speed 340 m/s


Here is how I would calculate the wavelength of the moving source. Modify it to calculate the velocity instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int main ()
{
	double w0, w1, v, c;
	int Sound;

	cout << "Is this sound or light? (1=sound, 2=light):";
	cin >> Sound;

	if (Sound == 1)
	{
		cout << "Sound selected" << endl;
		c = 340;
	}
	else
	{
		cout << "Light selected" << endl;
		c = 300000000;
	}

	cout << "Enter the wavelength of the stable source: ";
	cin >> w0;

	cout << "Enter the object's speed: ";
	cin >> v;

	w1 = w0*(1-v/c);

	cout << "The new doppler wavelength is " << w1 << endl;

	system ("pause");	
	return 0;
}


Also, please use code tags next time.
Last edited on
Topic archived. No new replies allowed.