开发者

Structure with formulas

My problem is that the correct value that is suppose to be stored in the data[i].Px isn't stored and the same with the data[i].Py. My formula in the开发者_开发技巧 do-while is faulty or must be. The formula should calculate the 'position' of a projectile. Vx and Vy is the initial velocities/values and Px and Py is the positions (in the x and y directions)

typedef struct
    {
        float t, Vx, Vy, Px, Py;

    }datapoint;

    steps = 100
    data[0].Vx = (20*(cos(30)));   //in my program 30 is in radians
    data[0].Vy = (20*(sin(30));
    data[0].Px = 0;
    data[0].Py = 0;

    do
    {   
                i=1;
            printf("Time: %.2f\t",i*q5);

            //X

            data[i].Vx = data[i-1].Vx;

            data[i].Px = ((data[i-1].Px) + ((data[i].Vx) * i));
            printf("X = %.2f\t",data[i].Px);

            //Y
            data[i].Vy= data[i-1].Vy - (9.81)*i;
            data[i].Py= data[i-1].Py + (data[i].Vy * i);
            printf("Y = %.2f\t", data[i].Py);

            printf("\n");
            i++;

    }while(**(data[i].Py >0)** && (i<=steps));


In the while condition of the do while loop, do you want to have

while((data[i].Py > 0) && (i <= steps));

Oh just saw a flaw. Why are you initializing i=1 inside the loop! Its value will never go beyond 2.

(I just skimmed through your question, so if this doesn't work I will check it thoroughly).


Judging from the notation used (since the declaration is not shown), data is an array of datapoint. Then data->Px is equivalent to data[0].Px.

You don't show how data[0] is initialized.

i never gets beyond 2. Unless steps is 2, the loop won't terminate because of the i <= steps condition. Since the value in data[0].Py (aka data->Py) is not modified in the loop, you have an infinite loop unless data[0].Py is negative or zero on entry to the loop.

You should look askance at a do { ... } while (...); loop. They aren't automatically wrong; but they are used far less often than while or for loops.

Generally, you will get better answers on StackOverflow if you produce a minimal compilable and runnable program (or a minimal non-compilable program that illustrates the syntax error if you are having problems making the program compilable). And 'minimal' means that nothing can be removed without altering the behaviour.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜