CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2013
    Posts
    2

    Unhappy 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();
    }

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: parabolic shot program BORLAND C

    Please use code tags when posting code.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    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 .

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: parabolic shot program BORLAND C

    Quote Originally Posted by greve View Post
    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: parabolic shot program BORLAND C

    Quote Originally Posted by s.mich View Post
    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.
    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
    Last edited by Paul McKenzie; November 14th, 2013 at 11:14 AM.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  7. #7
    Join Date
    Nov 2013
    Posts
    2

    Re: parabolic shot program BORLAND C

    thanks! it worked very well

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured