parabolic shot program BORLAND C
Hey! can you tell me what's wrong with my code?
it is suposed to calculate and graph time vs. distance. The problem I have is graphing, if I put too small values for Vi, which is the initial velocity, it just graphs one or two points, because t is too small. I alredy tried to adjust t with 'if(t<10) t=t*10; if (t>100) t=t/10;', But it seems not to solve my problem.
Other truble I have is that, as the graph is parabola, core values are very close together, and when plotted, these values are in the same line on the screen.
Here is my code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, y, hmax, t, amax, vi, v, a, b, g, ang; /*x= x coordinate, y= y coordinate, hmax= maximum height, amax= total distance on x axis, vi= initial velocity, ang= shot angle, a, b and c are just to make my life easier*/
g=9.81;
clrscr();
printf("Soy un programa que calcula un tiro parab¢lico y adem*s lo grafica\n");
printf("Dame el *ngulo de lanzamiento: ");
scanf("%f",&ang); //asking for shot angle
printf("Dame la velocidad inicial: ");
scanf("%f",&vi); //asking for initial velocity
hmax=(vi*sin(ang)*vi*sin(ang))/(2*g);
t=(2*vi*sin(ang))/g;
amax=vi*cos(ang)*t;
printf("El tiempo total es %f\nLa altura m*xima es %f\nEl alcance m*ximo es %f",t,hmax,amax); //printing out values
getch();
if(t>=100)
t=t/10;
if(t<=10)
t=t*10;
clrscr();
for(x=0;x<=t;x++) //here is the problem
{
y=vi*sin(ang)*x-(g*x*x)/2;
gotoxy(x,y);
printf("ø");
}
getch();
}
Re: parabolic shot program BORLAND C
Please use code tags when posting code.
Re: parabolic shot program BORLAND C
Hi,
where do clrscr() and gotoxy() come from (the .h-files you included don't contain declarations of them)?
Cheers,
Thomas
BTW: Your code would be easier to comprehend if you would use a structured formatting .
Re: parabolic shot program BORLAND C
Quote:
Originally Posted by
greve
Hi,
where do clrscr() and gotoxy() come from (the .h-files you included don't contain declarations of them)?
They are defined in conio.h for Borland c.
Re: parabolic shot program BORLAND C
Quote:
Originally Posted by
s.mich
Hey! can you tell me what's wrong with my code?
Borland C, as far as I remember, came with a debugger. You can find out yourself what is wrong with your code by debugging it using the debugger.
Quote:
The problem I have is graphing, if I put too small values for Vi, which is the initial velocity, it just graphs one or two points, because t is too small.
That isn't a coding problem -- that is a problem in your plan of calculating and plotting points on a console window.
You need to clearly figure out how to translate a point calculated with trig functions to a 25 x 80 character console so that the output looks "good". This should have been done before you wrote a single line of code. Maybe you need to restrict some of the variable values, scale them appropriately, etc. However, if I understand your issue, it has nothing to do with coding.
Regards,
Paul McKenzie
Re: parabolic shot program BORLAND C
As with any graphing... you need to properly scale and translate the coordinates in your graph to make best use of available screen size.
the simple explanation:
find your lowest (ymin) and highest (ymax) values in the graph.
the difference between the two should fit between your top (sytop) and bottom (sybot) screen coordinate.
calculate scale as:
yscale = (sytop-sybot) / (ymax-ymin)
so you need to multiply all your Y values with this yscale and add -ymin*yscale to translate the bottom of the graph to the bottom of the screen.
something similar to the x axis
Depending on the purpose, you may need additional work to have an equal xscale and yscale.
if your graph needs to have the zero axis visible, you may need to adjust for that as well.
there may be additional requirements that need you to account for stuff (such as aspect ratio)
this is a pretty broad topic.
Re: parabolic shot program BORLAND C
:) thanks! it worked very well