stupid problem. answer = 0.0

This is a discussion on stupid problem. answer = 0.0 within the C Programming forums, part of the General Programming Boards category; the code is pretty much a mess because i've been trying everything to figure this out on my own. every ...

  1. #1
    Cnoob
    Join Date
    Sep 2004
    Posts
    2

    stupid problem. answer = 0.0

    the code is pretty much a mess because i've been trying everything to figure this out on my own. every time i compile and test the program, no matter what values i put in i end up with 0.0 for the acceleration answer.

    i needed at least 1 user defined function for the problem i'm doing or else it would all be in the main.


    Code:
    #include <stdio.h>
    double calc_answer(double, double, double); /*User defined function*/
    double show_answer(double); /*User defined function*/
    int main(void)
    {
            double vi=0, /*initial velocity input in meters per second*/
            vf=0, /*final velocity input in meters per second*/
            time=0, /*time input in seconds*/
            accel1=0, /*acceleration calculation*/
            accel=0; /*acceleration output in meters per second squared*/
    
            printf("\n All values in seconds, meters per second, or meters per
    second squared");
            printf("\n Please enter the initial velocity:"); /*input initial
    velocity*/
            scanf("%lf", &vi);
            printf("\n Please enter the final velocity:"); /*input final
    velocity*/
            scanf("%lf", &vf);
            printf("\n Please enter the time:"); /*input time*/
            scanf("%f", &time);
    
            calc_answer(vi, vf, time);
            show_answer(accel);
            return(vi, vf, time);
    }
    double calc_answer(double vi, double vf, double time)
    {
            double accel1, /*acceleration part 1*/
            accel; /*final acceleration calculation value*/
            accel1 = vf-vi; /*Calculation of acceleration part 1*/
            accel = accel1/time; /*final calculation of acceleration*/
            return(accel);
    }
    double show_answer(double accel)
    {
            printf("\n The acceleration is %.1lf \n\n\n\n", accel);
            return;
    }
    any help will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    Code:
            scanf("%f", &time);
    
            accel = calc_answer(vi, vf, time);
            show_answer(accel);
    and i have no idea whats going on with the return statement.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>calc_answer(vi, vf, time);
    Where are you trapping the return value from this function?

    >>scanf("%f", &time);
    Are you sure that's right?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Your show_answer function can be be declared like this

    Code:
    void show_answer(double accel)
    {
        printf("\n The acceleration is %.1lf \n\n\n\n", accel);
    }
    Just remember to change the function prototype to match.

  5. #5
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19
    I dont think its really efficient to call a func to run just on line of code.
    You display of the answer could be included in the calculation function.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Depends on the situation. In this case it does not matter because it is a toy program. If the display was to be printed out many times, not dependent on the calculation, you would of course give it its own function. Something else to consider is that you ideally want your function to do one task, not many.

  7. #7
    Cnoob
    Join Date
    Sep 2004
    Posts
    2
    awesome! i got it. thanks alot guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid problem
    By Adrian20XX in forum C Programming
    Replies: 6
    Last Post: 08-23-2008, 08:16 AM
  2. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  3. an answer raises another problem...
    By Dorky King in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 03:11 PM
  4. Iterated function system fractal
    By OnionKnight in forum C Programming
    Replies: 17
    Last Post: 01-23-2007, 09:42 AM
  5. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 12:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21