Jump to content

c ++ programme on parabolic path

- - - - -

  • Please log in to reply
14 replies to this topic

#1
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I have a task that I have to submit immediately.OK,here are the instructions.
Creating a programme for parabolic path/parabolic motion.
-The object is shot from the ground and there must be 4 input datas : the duration of time (t),the angle,initial velocity and the time interval of the displacement(delta t).
-The output should be the displacement in both the x and y direction.
- projection time is set to 0
- the position after t seconds is calculated by delta t second intervals(t and delta t is also input after the program runs).
- the result is output to the file, use Excel to make graph with it

Here's what I have tried so far and there are many things that I am having problems with.
Somebody tell me how do I write out the equation correctly?And how do I define delta t?I know what it is but i don't know how to write that out.
And please,correct my mistakes.

#include <stdio.h>
#include <math.h>
#define g 9.8
int main()
{
double t, x, y, angle, Vo, deltat;
char buf[256];
int i;
FILE* fp;
printf( "t = " );
fgets( buf, 256, stdin );
sscanf( buf, "%lf\n", &t );
printf( "deltat = " );
fgets( buf, 256, stdin );
sscanf( buf, "%lf\n", &deltat );
printf("initial velocity = ");
sscanf( buf, "%lf\n", &Vo );
fgets( buf, 256, stdin );
printf( "angle = " );
sscanf( buf, "%lf\n", &angle );
fgets( buf, 256, stdin );

fp = fopen( "z:\\FreeFall.txt", "w" );
for( i = 0; i < t; i++ ) {
y = Vo*sin(angle)*deltat - ( g / 2.0 ) *deltat * deltat;
x = Vo*cos(angle)*deltat;


printf( "t= %f, y= %f ,x= %f\n", t, x, y );
fprintf( fp, "t=, %f, y=, %f ,x= %f\n", t, x, y );
}
fprintf( stderr, "File z:\\FreeFall.txt was created\n" );
fclose( fp );
return 0;
}

and this is the results I get.
Attached File  picture1.JPG   36.22K   56 downloads

Edited by aruwin, 11 December 2011 - 03:16 AM.


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
When reading velocity and angle, you have reversed order of sscanf and fgets function calls.


In your for loop, when you're printing out t, you have to remember that t will always be the same (whatever the user provided), so instead you should print i, which should be incremented by Δt.


printf( "t= %f, y= %f ,x= %f\n", t, x, y );
You have x and y mixed up.


Lastly, the reason you're getting negative numbers is about angle.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Quote

Flying Dutchman;320185
In your for loop, when you're printing out t, you have to remember that t will always be the same (whatever the user provided), so instead you should print i, which should be incremented by Δt.



I have changed it but the results are the same and the only thing different is that the results come out endless :(
#include <stdio.h>
#include <math.h>
#define g 9.8
int main()
{
double t, x, y, angle, Vo, deltat;
char buf[256];
int i;
FILE* fp;
printf( "t = " );
fgets( buf, 256, stdin );
sscanf( buf, "%lf\n", &t );
printf( "deltat = " );
fgets( buf, 256, stdin );
sscanf( buf, "%lf\n", &deltat );
printf("initial velocity = ");
sscanf( buf, "%lf\n", &Vo );
fgets( buf, 256, stdin );
printf( "angle = " );
sscanf( buf, "%lf\n", &angle );
fgets( buf, 256, stdin );

fp = fopen( "z:\\FreeFall.txt", "w" );
for( i = 0; i < t; 'i+=deltat' ) {
y = Vo*sin(angle)*deltat - ( g / 2.0 ) *deltat * deltat;
x = Vo*cos(angle)*deltat;


printf( "i= %f, y= %f ,x= %f\n", t, y, x );
fprintf( fp, "i=, %f, y=, %f ,x= %f\n", t, y, x );
}
fprintf( stderr, "File z:\\FreeFall.txt was created\n" );
fclose( fp );
return 0;
}

Edited by aruwin, 11 December 2011 - 06:49 AM.


#4
Brad7533

Brad7533

    Newbie

  • Members
  • Pip
  • 4 posts
the only thing i noticed wrong was the } at the end.

}
fprintf( stderr, "File z:\\FreeFall.txt was created\n" );
fclose( fp );
return 0;
} <--- you already specified the end of the code 4 lines up

---------- Post added at 10:04 AM ---------- Previous post was at 10:00 AM ----------

also you are ending the program immediately (return 0) means stop the program. try right before

do this instead
cin.get ();
return 0;

#5
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Brad7533 said:

the only thing i noticed wrong was the } at the end.

}
fprintf( stderr, "File z:\\FreeFall.txt was created\n" );
fclose( fp );
return 0;
} <--- you already specified the end of the code 4 lines up

---------- Post added at 10:04 AM ---------- Previous post was at 10:00 AM ----------

also you are ending the program immediately (return 0) means stop the program. try right before

do this instead
cin.get ();
return 0;

But I don't think there's any problem with the "}" at the end because it is the closing for the "{" at the very beginning.

And I I have added cin.get() before return o but there's an error: 'cin' was not declared in this scope
But what is cin.get?I haven't learned that one yet.

#6
Brad7533

Brad7533

    Newbie

  • Members
  • Pip
  • 4 posts
cin.get () just tells the program not to quit. i looked at the result of your code. the pic up there. im not getting it whats the problem with the program.

---------- Post added at 10:17 AM ---------- Previous post was at 10:16 AM ----------

but you only use it when you use <iostream> so ignore that.

---------- Post added at 10:20 AM ---------- Previous post was at 10:17 AM ----------

try a different angle

#7
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Brad7533 said:

cin.get () just tells the program not to quit. i looked at the result of your code. the pic up there. im not getting it whats the problem with the program.

---------- Post added at 10:17 AM ---------- Previous post was at 10:16 AM ----------

but you only use it when you use <iostream> so ignore that.

u see that the results came out repetitively?The results should be something like this:

so lets say if I input t=5 and delta t=2,the results should be
t=0.00 x=some numbers,y=some numbers
t=2.00 x=some numbers,y=some numbers
t=4.00 x=some numbers,y=some numbers
t=6.00 x=some numbers,y=some numbers
t=8.00 x=some numbers,y=some numbers

#8
Brad7533

Brad7533

    Newbie

  • Members
  • Pip
  • 4 posts
the program is correct. if you want a different result like not negative then you need a different equation.

#9
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I have tried many different angles but I think the problem here is the t and delta t.I don't know the correct way to write it out.

#10
Brad7533

Brad7533

    Newbie

  • Members
  • Pip
  • 4 posts
i have never used delta t. in my programming. so i dont know how to write it sorry. but if the program is running then it would not be a syntax error in the way it was written.

#11
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Brad7533 said:

i have never used delta t. in my programming. so i dont know how to write it sorry. but if the program is running then it would not be a syntax error in the way it was written.

well,it's not actually the way to write it out.What I mean is how to get it to work.I don't know what is wrong with the increments :/
As you can see the changed I have made from the first one and the second one that I posted,I think there is something wrong there.
Have you made a programme on parabolic path?

#12
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Someone,please help me until I get this one done.This is my recent code and the results did not turn out good.
I wanna make the program accept my t,deltat,Vo,and angle but I have problems with the t and deltat.And also,there is one more problem.I tried calculating the displacement manually to see if wrote the equation into the program correctly but the answers were different!!Please correct my equations,please.I know that is the correct formula but perhaps the way I code it is wrong,the arrangement or something.

Lets say I put 3 for t and 2 for deltat,the result I want is supposed to be like this:

t = 0.00 x=(some number) y = (some number)
t = 2.00 x=(some number) y = (some number)
t = 4.00 x=(some number) y = (some number)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define g 9.8
#define PI 3.142
int main()
{
double t, x, y, angle, Vo, deltat;
char buf[256];
int i;
FILE* fp;
printf( "\nt = " );
scanf("%lf",&t);

printf( "\ndeltat = " );
scanf("%lf",&deltat);

printf("\ninitial velocity = ");
scanf("%lf", &Vo);

printf( "\nangle = " );
scanf("%lf",&angle);

fp = fopen( "z:\\FreeFall.txt", "w" );
for( i = 0; i < t; 'i+=deltat' ) {
t = i*deltat;
y = Vo*sin(angle*PI/180)*deltat - ( g / 2.0 ) *deltat * deltat;
x = Vo*cos(angle*PI/180)*deltat;


printf( "t= %f, y= %f ,x= %f\n", t, y, x );
fprintf( fp, "t=, %f, y=, %f ,x= %f\n", t, y, x );
}
fprintf( stderr, "File z:\\FreeFall.txt was created\n" );
fclose( fp );
return 0;
}

And the results became like this
Attached File  results.JPG   21.59K   10 downloads

Attached Files


Edited by aruwin, 12 December 2011 - 01:54 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users