Problem of the code?

Discussion in 'C Programming' started by MMHernandes, Mar 13, 2019.

  1. MMHernandes

    MMHernandes

    Joined:
    Mar 13, 2019
    Messages:
    2
    Likes Received:
    0
    Code (C):
    #include<stdio.h>
    int main( void )
    {
        int sum = 0;
        float average;
        int weight_1, weight_2;
        printf( "1.weight> " );
        scanf( "%d", &weight_1 );
        sum = sum + weight_1;
        printf( "2.weight> " );
        scanf( "%d", &weight_2 );
        sum = sum + weight_2;
        average = sum / 2.0;
        printf( "Average: %f\n", &average );
        return 0;
    }
    When i run, code gives the average as 0.00000000. What's the problem?
     
    Last edited by a moderator: Mar 14, 2019
    MMHernandes, Mar 13, 2019
    #1
    1. Advertisements

  2. MMHernandes

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    110
    Likes Received:
    14
    Location:
    Malaysia
    Why you need to use &average in printf( "Average: %f\n", &average ); ?
     
    John Joe, Mar 17, 2019
    #2
    1. Advertisements

  3. MMHernandes

    MMHernandes

    Joined:
    Mar 13, 2019
    Messages:
    2
    Likes Received:
    0
    Thank you. :D
     
    MMHernandes, Apr 12, 2019
    #3
  4. MMHernandes

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    110
    Likes Received:
    14
    Location:
    Malaysia
    welcome
     
    John Joe, Apr 14, 2019
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.