## 267 - Of(f) Course!

Moderator: Board moderators

Orgi
New poster
Posts: 11
Joined: Mon Oct 29, 2001 2:00 am
Location: Bulgaria
I have solved it but i get WA.
Could someone tell me some tips or tricks in this one?

Thank you.

C8H10N4O2
Experienced poster
Posts: 137
Joined: Wed Feb 27, 2002 2:00 am
Did you remember to round the floating point properly? double(long(Answer*100+0.5))/100 Floating points get nasty. Often answers will be 4.999998 instead of 5.000000.

hlchan
New poster
Posts: 7
Joined: Sat May 25, 2002 8:15 am

### My Observation

After about 10 submission, i finally get Accepted(PE).....

My observations:
1. input speeds are greater than zero
2. there is always solutions that lead your plane to fly along the desired direction, although you may flying backwards. (consider the set of data '100 270 0 1', you can never fly the direction you want, even backward...)
3. Take care if you are using math division. Division by a small number may lead to round off error.
4. Take care about presentation of '-0.0001' and '359.999'

Personally, i think this question is not so good. Too much on presentation requirement rather than algorithm.... may be i am stupid...

PMNOX
New poster
Posts: 49
Joined: Wed Feb 13, 2002 2:00 am
Location: Poland
Contact:

### 267 - Of(f) Course!

i'm trying to calculate ground_speed, but for some reason, it doesn't work correctly

Code: Select all

``````
#include <iostream.h>
#include <stdio.h>
#include <math.h>

#ifndef ONLINE_JUDGE
#include <fstream.h>
ifstream fcin("267.txt");
#define cin fcin

#endif

#define pi  3.1415926535897932384626433832795

int main()
{
double wind_speed, wind_direction, desired_course,air_speed;
cin>>wind_speed;
int n=0;
while(!cin.eof())
{
cin>>wind_direction>>desired_course>>air_speed;
if(n++>0)printf("\n");
printf("WIND SPEED %0.2lf\n",wind_speed);
printf("WIND DIRECTION %0.2lf\n",wind_direction);
printf("DESIRED COURSE %0.2lf\n",desired_course);
printf("TRUE AIRSPEED %0.2lf\n",air_speed);

//#ifndef ONLINE_JUDGE
GROUND_SPEED = 0;
//#endif
if(wind_direction >=180.00)
wind_direction -= 180;
else
wind_direction += 180;

double x=air_speed*sin(desired_course*(pi/180))+wind_speed*sin(wind_direction*(pi/180));
double y=air_speed*cos(desired_course*(pi/180))+wind_speed*cos(wind_direction*(pi/180));
GROUND_SPEED = sqrt(x*x+y*y);

printf("GROUND SPEED %0.2lf\n",GROUND_SPEED);

cin>>wind_speed;
}
return 0;
}``````

kate
New poster
Posts: 11
Joined: Thu May 22, 2003 9:37 pm

### 267

I solved this problem and it was accepted except I had to make it output what I think is the wrong answer.

WIND SPEED 15.00
WIND DIRECTION 290.00
DESIRED COURSE 260.00
TRUE AIRSPEED 100.00
GROUND SPEED 86.73

Looking at that example, it says the wind is moving at 290 degrees and the desired course is 260. So, I would think the aircraft heading would be somewhere less than 260 to compensate for the wind in the opposite direction. Sure enough, the numbers look fine that way to me except if I find that the aircraft heading should be, say, 10 degrees clockwise of the desired course, the actual solution is 10 degrees counter-clockwise. Which would cause both the wind and the aircraft heading to be moving the plane counter-clockwise of it's desired course. It's adding up instead of cancelling out. At least that's how I'm interepreting the question.

epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.
obviously wind direction 290 means wind *comes from* 290.
this is why ground speed is less than true air speed. (wind comes from the front, not back)
so one would expect aircraft heading to always be between desired course and wind speed. (unless the difference is greater than 180 then it's not)

best regards.
We never perform a computation ourselves, we just hitch a ride on the great Computation that is going on already. --Tomasso Toffoli

kate
New poster
Posts: 11
Joined: Thu May 22, 2003 9:37 pm
Hmmm, that would explain it but it still seems strange. If a plane was travelling "south" and the wind direction was "north", I would think the wind was moving in the opposite direction of the plane. Oh well.

Andy
New poster
Posts: 1
Joined: Mon May 26, 2003 12:47 am
Contact:

### Invalid Memory reference...

Anyone know what this means?

Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

I think it's in reference to the way I terminated my input loop, but I can't figure out how else to do it. I've tried while(!cin.eof()) and while(cin) but neither seems to work....any ideas?

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
Could anyone tell me , why this program got WA ?
I'ii be very greatful for any IO sample, on which ths program fails... (with correct output) )

Best reagrds
DM

Code: Select all

``````#include <stdio.h>
#include <math.h>

#define DEG		(PI / 180.0)

int main(void)
{
double WS,alfa2,alfa1,AS,x,x1,x2,beta,ASx,ASy,delta,alfa,PI;
int first = 1;

PI = acos(-1.0);
while(scanf("%lf %lf %lf %lf",&WS,&alfa2,&alfa1,&AS) == 4)
{
if(first)
first = 0;
else
printf("\n");
printf("WIND SPEED %.2lf\n",WS);
printf("WIND DIRECTION %.2lf\n",alfa2);
printf("DESIRED COURSE %.2lf\n",alfa1);
printf("TRUE AIRSPEED %.2lf\n",AS);
x1 = beta = fabs(alfa1 - alfa2);
beta = alfa1 + x2;
if(beta < 0.0) beta += 360.0;
while(beta >= 360.0) beta -= 360.0;
if(x2 < 0.0) x2 += 360.0;
if(x2 >= 180.0) x2 -= 180.0;
x2 *= DEG;
x = WS*sin(DEG*x1 - x2)/sin(x2);
if((fabs(beta) < 0.005) || (beta >= 359.995)) beta = 0.0;
if(fabs(x) < 0.005) x = 0.0;
printf("GROUND SPEED %.2lf\n",x);
}
return 0;
}
``````
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

LittleJohn
Learning poster
Posts: 83
Joined: Wed Feb 27, 2002 2:00 am
Location: Taiwan
I found that the input speeds may be negative.
Could somebody tell me what's the output for the following inputs?

Code: Select all

``````-15 110 260 100
23 294 319 19``````
thx

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Hi Dominik,

Code: Select all

``````x = WS*sin(DEG*x1 - x2)/sin(x2);
``````
Could be undefined if sin(x2) is 0;

INTPUT

Code: Select all

``````15.0 260.0 260.0 101.73
``````
OUTPUT

Code: Select all

``````WIND SPEED 15.00
WIND DIRECTION 260.00
DESIRED COURSE 260.00
TRUE AIRSPEED 101.73
GROUND SPEED 86.73
``````
Can't confirm because I myself haven't gotten AC.

kenneth
New poster
Posts: 24
Joined: Wed Mar 02, 2005 12:29 am
Hi All

I think I have a problem of understanding the output. Can anyone help?

For the second sample input

15.0 270.0 135.0 200.0

We have the following diagram

15 (wind)
---->
\
\ 200 (plane relative to air)
\

Since we know the velocity of plane relative to ground = velocity of plane relative to wind + velocity of wind.

The speed of the plane should be sqrt((15 + 200 cos45)^2 + (200 sin45)^2) = 210.87????

Obviously, that's not the sample output.

Can anyone please explain to me which bit did I get it wrong?

Thanks

mukeshtiwari
Learning poster
Posts: 63
Joined: Tue Mar 07, 2006 6:51 pm
Location: india

### WA:267

i solved the prob 267 but i m getting WA plz help me .
here is my code ....

Code: Select all

``````#include<stdio.h>
#include<math.h>

main()
{
double  vwg,thetaw,thetar,vaw,theta,alpha,phi,v,p,q,r,s;
while(scanf("%lf%lf%lf%lf",&vwg,&thetaw,&thetar,&vaw)!=EOF)
{       p=vwg;
q=thetaw;
r=thetar;
s=vaw;
thetaw = thetaw - 180.00;
if(thetaw<0)
thetaw=360.00+thetaw;

if(thetaw > thetar)
theta=thetaw-thetar;
else
theta=thetar-thetaw;
if(theta > 180)
theta=360-theta;

alpha=theta+(180/3.14)*asin((vwg/vaw)*sin((3.14*theta)/180));

if(thetaw > thetar)
{
if(thetaw-thetar > 180)
phi=thetaw+alpha-180;
else
phi=thetaw-alpha;
if(phi<0)
phi=360+phi;
}
else
{
if(thetar - thetaw > 180)
phi=360+thetaw-alpha;
else
phi=alpha+ thetaw;
if(phi>360)
phi=phi - 360;
}

v=sqrt(vwg*vwg+vaw*vaw+2*vwg*vaw*cos((alpha*3.14)/180));
printf("WIND SPEED %.2lf\n",p);
printf("WIND DIRECTION %.2lf\n",q);
printf("DESIRED COURSE %.2lf\n",r);
printf("TRUE AIRSPEED  %.2lf\n",s);
printf("GROUND SPEED %.2lf\n\n",v);
}
}``````

mukeshtiwari
Learning poster
Posts: 63
Joined: Tue Mar 07, 2006 6:51 pm
Location: india
i again try with this code by improving the value of pi
but still WA

Code: Select all

``````#include<stdio.h>
#include<math.h>
#define PI 3.141592653589793115997963468544185161590576171875

main()
{
double  vwg,thetaw,thetar,vaw,theta,alpha,phi,v,p,q,r,s;
while(scanf("%lf%lf%lf%lf",&vwg,&thetaw,&thetar,&vaw)!=EOF)
{
p=vwg,q=thetaw,r=thetar,s=vaw;
thetaw = thetaw - 180.00;
if(thetaw<0)
thetaw=360.00+thetaw;
if(thetaw > thetar)
theta=thetaw-thetar;
else
theta=thetar-thetaw;
if(theta > 180)
theta=360-theta;
alpha=theta+(180/PI)*asin((vwg/vaw)*sin((PI*theta)/180));
if(thetaw > thetar)
{
if(thetaw-thetar > 180)
phi=thetaw+alpha-180;
else
phi=thetaw-alpha;
if(phi<0)
phi=360+phi;
}
else
{
if(thetar - thetaw > 180)
phi=360+thetaw-alpha;
else
phi=alpha+ thetaw;
if(phi>360)
phi=phi - 360;
}

v=sqrt(vwg*vwg+vaw*vaw+2*vwg*vaw*cos((alpha*PI)/180));
printf("WIND SPEED %.2lf\n",p);
printf("WIND DIRECTION %.2lf\n",q);
printf("DESIRED COURSE %.2lf\n",r);
printf("TRUE AIRSPEED %.2lf\n",s);
printf("GROUND SPEED %.2lf\n\n",v);
}
}``````

mukeshtiwari
Learning poster
Posts: 63
Joined: Tue Mar 07, 2006 6:51 pm
Location: india
hi everybody
i tried to solve this but keep getting WA i checked it for lot of inputs but i m not geeting the error .kindly give some input of accpted program...
here is my code plz check this....

Code: Select all

``````#include<stdio.h>
#include<math.h>
#define PI 3.141592653589793115997963468544185161590576171875

main()
{
double  vwg,thetaw,thetar,vaw,theta,alpha,phi,v,p,q,r,s;
while(scanf("%lf%lf%lf%lf",&vwg,&thetaw,&thetar,&vaw)!=EOF)
{
p=vwg,q=thetaw,r=thetar,s=vaw;
thetaw = thetaw - 180.00;
if(thetaw<0)
thetaw=360.00+thetaw;
if(thetaw > thetar)
theta=thetaw-thetar;
else
theta=thetar-thetaw;
if(theta > 180)
theta=360-theta;
alpha=theta+(180/PI)*asin((vwg/vaw)*sin((PI*theta)/180));
if(thetaw > thetar)
{
if(thetaw-thetar > 180)
phi=thetaw+alpha-180;
else
phi=thetaw-alpha;
if(phi<0)
phi=360+phi;
}
else
{
if(thetar - thetaw > 180)
phi=360+thetaw-alpha;
else
phi=alpha+ thetaw;
if(phi>360)
phi=phi - 360;
}

v=sqrt(vwg*vwg+vaw*vaw+2*vwg*vaw*cos((alpha*PI)/180));
printf("WIND SPEED %.2lf\n",p);
printf("WIND DIRECTION %.2lf\n",q);
printf("DESIRED COURSE %.2lf\n",r);
printf("TRUE AIRSPEED %.2lf\n",s);