Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,972 people online right now. Registration is fast and FREE... Join Now!




Projectile Motion: calculating the X distance using C

 
Reply to this topicStart new topic

Projectile Motion: calculating the X distance using C

zizo
12 Jan, 2008 - 05:21 AM
Post #1

New D.I.C Head
*

Joined: 12 Jan, 2008
Posts: 3

Hi there, i was tryig to make a program that calculates the horizontal distance of a projectilemotion usig the following four formulas:

angleRad)= angleDeg*(PI/180)
Vx=Vinitial*cos(angleRad)
Vy=Vinitial*sin(angleRad)
HorizontalDistance=Vx*2*(Vy/angleRad)

using four different user defied functions. please hav a look at my code and tell me where is the problem as i hav tried very hard to figure this out.

Thak You Very much

CODE:

CODE

#include<stdio.h>
#include<math.h>
#define PI 3.1423
#define GRAV 9.81
double ALPHA(double);
double V_HOR(double);
double V_VER(double);
double RESULT(double);

void main()

{
int i;
double ang[5],vel[5],vxy;
    for(i=0;i<5;i++)
    {
        printf("PLease enter the agle and initial velocity\n");
        scanf("%f %f",&ang[i],&ang[i]);
    }

    printf("******************The results are************************\n");
    printf("ANG\tVelocity\tDISTANCE\n");
    printf("---------------------------------------------------------\n");
    printf("%f\t%f\t%f",RESULT(vxy));

    {
        double ALPHA(double a);
        ALPHA = ang[i]*(PI/180);
        return (ALPHA);
    }
    {
        double V_HOR(double vx);
        V_HOR = vel[i]*cos(ALPHA);
        return (V_HOR);
    }
    {
        double V_VER(double vy);
        V_VER = vel[i]*sin(ALPHA);
        return (V_VER);
    }
    {
        double RESULT(double vxy);
        RESULT = V_HOR*2*(V_VER / ALPHA);
        return (RESULT);
    }

}


This post has been edited by zizo: 12 Jan, 2008 - 05:28 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 05:27 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Have a look here for information on the syntax of writing and using functions.

http://www.cprogramming.com/tutorial/c/lesson4.html

(edit - corrected URL)

This post has been edited by Bench: 12 Jan, 2008 - 05:29 AM
User is offlineProfile CardPM
+Quote Post

zizo
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 10:05 AM
Post #3

New D.I.C Head
*

Joined: 12 Jan, 2008
Posts: 3

QUOTE(Bench @ 12 Jan, 2008 - 06:27 AM) *

Have a look here for information on the syntax of writing and using functions.

http://www.cprogramming.com/tutorial/c/lesson4.html

(edit - corrected URL)


wow that was a very quick reply, hehe....

but isnt my function declaration and prototype and function call and type all correct? i'm a begginner, so i dont understand the errors are sayin....something wrong with the function parameters.....please see if you can spot the error

thanx
zizo
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 12:08 PM
Post #4

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,463



Thanked: 10 times
Dream Kudos: 325
My Contributions
Just a few things.

* You can't define functions inside of your main method.

* Your main method can't be void main(), it has to be int main(), and it must return either EXIT_SUCCESS or EXIT_FAILURE.

* You should almost always include <stdlib.h>.

* Functions and variables should always be lower case, upper case is typically reserved for preprocessor macros.

* You really should read up on functions an scope. smile.gif
User is offlineProfile CardPM
+Quote Post

Bench
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 04:21 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(zizo @ 12 Jan, 2008 - 06:05 PM) *

but isnt my function declaration and prototype and function call and type all correct? i'm a begginner, so i dont understand the errors are sayin....something wrong with the function parameters.....please see if you can spot the error
No, they're not - that's why I pasted the link. I strongly suggest you read it carefully and try the example program out for yourself in your IDE/compiler. Pay attention to the layout of the program, including the positioning of brackets/parenthesis, and where functions appear in the program relative to other parts of the program - then compare that closely with your program.


QUOTE(Tom9729 @ 12 Jan, 2008 - 08:08 PM) *
it must return either EXIT_SUCCESS or EXIT_FAILURE.
main can return any int value - though, the meaning of the return value is O/S dependent

QUOTE(Tom9729 @ 12 Jan, 2008 - 08:08 PM) *

* You should almost always include <stdlib.h>.
You should include whichever header files you need, and omit those which you don't need. including a header for the sake of it doesn't really benefit anyone

QUOTE(Tom9729 @ 12 Jan, 2008 - 08:08 PM) *

* Functions and variables should always be lower case, upper case is typically reserved for preprocessor macros.
This is a style issue. Whilst I personally would not advocate use of all-caps function names, there are no hard and fast rules on coding style, and advice offering "should always" guidelines on any coding style must be taken with a large pinch of salt. Such guidelines inevitably lead to disagreements and so-called "holy" wars between programmers with differing personal tastes. Consistency and overall readability are far bigger than whether a name is in upper/lower/mixed case IMHO.

This post has been edited by Bench: 12 Jan, 2008 - 04:26 PM
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 06:33 PM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,463



Thanked: 10 times
Dream Kudos: 325
My Contributions
QUOTE(Bench @ 12 Jan, 2008 - 05:21 PM) *

main can return any int value - though, the meaning of the return value is O/S dependent

Bad choice of words then, but you should have main returning EXIT_SUCCESS or EXIT_FAILURE. I don't know of any platforms that actually do this, but from what I understand on some platforms return 0 would actually indicate an failure, hence the use of preprocessor macros (each system's preprocessor will be updated to ensure that EXIT_SUCCESS is replaced with the right value).

Kind of what you said I guess, just in more words. smile.gif

QUOTE(Bench @ 12 Jan, 2008 - 05:21 PM) *

You should include whichever header files you need, and omit those which you don't need. including a header for the sake of it doesn't really benefit anyone

Maybe I'm just doing something wrong, but I find that if I don't include stdlib.h but I'm using functions from the standard library (and I think it's safe to say that most programs do) I get warnings when trying to compile (despite the fact that the functions work fine).

CODE

tom@midnight:/tmp$ cc -o test test.c -Wall -lm
test.c: In function ‘main’:
test.c:5: warning: implicit declaration of function ‘abs’


Including stdlib.h ensures that those warnings don't show up. smile.gif
User is offlineProfile CardPM
+Quote Post

zizo
RE: Projectile Motion: Calculating The X Distance Using C
12 Jan, 2008 - 08:16 PM
Post #7

New D.I.C Head
*

Joined: 12 Jan, 2008
Posts: 3

man i did read the link that u gave me and i replaced all my function names to lower case, and added stdlib.h but it did not even erduce a warning......hehehehe

i still get these errors and warnings:
--------------------Configuration: Text1 - Win32 Debug--------------------
Compiling...
Text1.c
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(29) : error C2115: '=' : incompatible types
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(29) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(29) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(30) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(30) : warning C4098: 'main' : 'void' function returning a value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(34) : error C2115: 'function' : incompatible types
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(34) : warning C4024: 'cos' : different types for formal and actual parameter 1
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(34) : error C2115: '=' : incompatible types
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(34) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(34) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(35) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(35) : warning C4098: 'main' : 'void' function returning a value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(39) : error C2115: 'function' : incompatible types
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(39) : warning C4024: 'sin' : different types for formal and actual parameter 1
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(39) : error C2115: '=' : incompatible types
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(39) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(39) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(40) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(40) : warning C4098: 'main' : 'void' function returning a value
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(44) : error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double )'
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(44) : error C2296: '/' : illegal, left operand has type 'double (__cdecl *)(double )'
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(44) : error C2297: '/' : illegal, right operand has type 'double (__cdecl *)(double )'
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(45) : warning C4550: expression evaluates to a function which is missing an argument list
C:\Documents and Settings\SuAdDaRrH\Desktop\New Folder\Text1.c(45) : warning C4098: 'main' : 'void' function returning a value
Error executing cl.exe.

Text1.exe - 11 error(s), 13 warning(s)

User is offlineProfile CardPM
+Quote Post

Bench
RE: Projectile Motion: Calculating The X Distance Using C
13 Jan, 2008 - 04:26 AM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(zizo @ 13 Jan, 2008 - 04:16 AM) *

man i did read the link that u gave me and i replaced all my function names to lower case, and added stdlib.h but it did not even erduce a warning......hehehehe
None of those things are related to your error - did you actually look closely at the example program/s in the link? Specifically, the positioning of functions and brackets?

Look at this
CODE
/* add prototype */
int add(int x, int y);

/* main function */
int main()
{
    int result = add(3,4);
}  /* End of main */

/* add function */
int add(int x, int y)
{
    int num = x + y;
    return num;
} /* end of add */


Compare and contrast with your program
CODE
    {
        double RESULT(double vxy);
        RESULT = V_HOR*2*(V_VER / ALPHA);
        return (RESULT);
    }

}
Look at three things -
- Where the function name appears in relation to its brackets which enclose the body of that function
- Where you've attempted to assign a value to the function name (You can't do this).
- Where the function appears in your program in relation to the ending brace of main.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:23AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month