Thread: Function and arguement help

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20

    Function and arguement help

    I am new to C programming and having a hard time writing this program. If somebody can help it would great. This program ask the user for initial velocity and initial angle, then must calculate the max distance, max height and total travel time. The "requirements " must use at least 3 function not including the main function.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int totaltraveltime(float velocity,float angle,float gravity); 
    int peakheight(float velocity,float angle,float gravity);
    int range(float velocity,float angle,float gravity);
    
    
    int main(void)
    {
        float initial_velocity=0;
        float initial_angle=0;
        float gravity=9.8;
        float max_distance;
        float max_height;
        float duration;
        
        printf("please enter initial velocity\n");
        
        scanf("%f",&initial_velocity);
        
        printf("please enter initial angle\n");
        
        scanf("%f",&initial_angle);
        
        max_distance = range(initial_velocity,initial_angle,gravity);
        
        max_height = peakheight(initial_velocity,initial_angle,gravity);
        
        duration = totaltraveltime(initial_velocity,initial_angle,gravity);
        
        
      system("PAUSE");	
      return 0;
    }
         
         int totaltraveltime(float velocity,float angle)
     { 
         duration= 2*((initial_velocity)*sin(initial_angle))/gravity;
    
         return duration;
     }
         int peakheight(float velocity,float angle,float gravity)
     {
         max_height=((initial_velocity)*sin(pow)(initial_angle))/(2*gravity);
        
        return max_height;
     } 
        int range(float velocity,float angle,float gravity)
     {   
          max_distance=((pow)(initial_velocity)*sin(2*initial_angle))/gravity;
      
        return max_distance;
     }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So What's the problem?


    Code:
    int totaltraveltime(float velocity,float angle,float gravity); 
    
    
         int totaltraveltime(float velocity,float angle)
     { 
         duration= 2*((initial_velocity)*sin(initial_angle))/gravity;
    
         return duration;
     }
    Read your text book on functions!!!!

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20

    The text book assign to the class is Matlab.

    The text book that is issued to the class is for Matlab. My teacher don't want us to use Matlab yet. So most information i get is over the internet

    Quote Originally Posted by timbean View Post
    I am new to C programming and having a hard time writing this program. If somebody can help it would great. This program ask the user for initial velocity and initial angle, then must calculate the max distance, max height and total travel time. The "requirements " must use at least 3 function not including the main function.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int totaltraveltime(float velocity,float angle,float gravity); 
    int peakheight(float velocity,float angle,float gravity);
    int range(float velocity,float angle,float gravity);
    
    
    int main(void)
    {
        float initial_velocity=0;
        float initial_angle=0;
        float gravity=9.8;
        float max_distance;
        float max_height;
        float duration;
        
        printf("please enter initial velocity\n");
        
        scanf("%f",&initial_velocity);
        
        printf("please enter initial angle\n");
        
        scanf("%f",&initial_angle);
        
        max_distance = range(initial_velocity,initial_angle,gravity);
        
        max_height = peakheight(initial_velocity,initial_angle,gravity);
        
        duration = totaltraveltime(initial_velocity,initial_angle,gravity);
        
        
      system("PAUSE");	
      return 0;
    }
         
         int totaltraveltime(float velocity,float angle)
     { 
         duration= 2*((initial_velocity)*sin(initial_angle))/gravity;
    
         return duration;
     }
         int peakheight(float velocity,float angle,float gravity)
     {
         max_height=((initial_velocity)*sin(pow)(initial_angle))/(2*gravity);
        
        return max_height;
     } 
        int range(float velocity,float angle,float gravity)
     {   
          max_distance=((pow)(initial_velocity)*sin(2*initial_angle))/gravity;
      
        return max_distance;
     }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    Is that the way I need to define the prototype?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20

    Please help me someone

    I do not know what I am doing wrong =(

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A function prototype looks like the function definition (except it doesn't have the braces with the function body). So, if you have a function that takes one int and returns one, its prototype might look like:
    Code:
    int foo( int );
    Then the actual function may look something like:
    Code:
    int foo( int x )
    {
       ...do stuff with x
       return someint;
    }
    If you have a specific question, please ask as specific as you can. General questions get general answers. Specific questions are more likely to get specific answers.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20

    Here is my problem

    1. When i compile I get a error message on line 40. 'conflicting types for totaltraveltime'


    2. I have my formula = to duration.

    Code:
     int totaltraveltime(float velocity,float angle)
         { 
               duration= 2*((initial_velocity)*sin(initial_angle))/gravity;
    
              return duration;
         }

    Will the value i get go inside the "main function"?

    Code:
     duration = totaltraveltime(initial_velocity,initial_angle,gravity);

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea where line 40 is, but at a rough guess:
    Code:
    int totaltraveltime(float velocity,float angle,float gravity);
    Code:
    int totaltraveltime(float velocity,float angle)
    Do those look the same to you?

    And that's what return does -- it returns that value to the caller. You don't have to use the same name if you don't want to, as the duration that's inside the function and the duration that's inside main aren't going to be related at all anyway.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    Thank You very much. I did not see that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use void* as an arguement?
    By Kleid-0 in forum C Programming
    Replies: 9
    Last Post: 11-25-2010, 11:29 PM
  2. function with array arguement
    By timhxf in forum C Programming
    Replies: 5
    Last Post: 01-05-2007, 03:20 PM
  3. passing a vector as an arguement
    By gL_nEwB in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2006, 10:11 PM
  4. function call as a 'case' arguement
    By rc7j in forum C Programming
    Replies: 2
    Last Post: 02-17-2002, 08:09 PM
  5. an intersting arguement
    By iain in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 01-21-2002, 01:39 AM