Like Tree1Likes
  • 1 Post By TheBigH

'Type double' help D:

This is a discussion on 'Type double' help D: within the C Programming forums, part of the General Programming Boards category; I'm doing an assignment for an Intro to C class and one of the problems assigned from the book requires ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    'Type double' help D:

    I'm doing an assignment for an Intro to C class and one of the problems assigned from the book requires me to:

    -Write a program that prompts the user to enter three numbers
    -Prints the three numbers vertically, first forward and then reversed.

    Simple enough, my code for that is
    Code:
    #include <stdio.h>
    
    int main()
    {
    // Local Declarations
        int a, b, c;
    
    // Statements
        printf("Please enter three numbers: \n");
        scanf("%d %d %d", &a, &b, &c);
    
        printf("\n\nYour numbers forward: \n%d \n%d \n%d\n\n", a, b, c);
        printf("Your numbers reversed: \n%d \n%d \n%d", c, b, a);
    
        return 0;
    
    }
    and the output is what I thought was required...
    Please enter three numbers:
    7 77 777


    Your numbers forward:
    7
    77
    777

    Your numbers reversed:
    777
    77
    7
    I look back at the HW requirements and it turns out for this particular problem, my professor wants me to write more stuff; "Declare the three numbers of type double. Format the output: xxx.xx (such as 200.99, 50.25, etc.)."

    Don't really have any idea on what to do for this... I changed the int to double but my output gets really weird.

    This is the new code (with int changed to double)
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    // Local Declarations
        double a, b, c;
    
    // Statements
        printf("Please enter three numbers: \n");
        scanf("%d %d %d", &a, &b, &c);
    
        printf("\n\nYour numbers forward: \n%d \n%d \n%d\n\n", a, b, c);
        printf("Your numbers reversed: \n%d \n%d \n%d", c, b, a);
    
        return 0;
    
    }
    This is the output with said-new code
    Please enter three numbers:
    5 10 15


    Your numbers forward:
    5
    4200358
    10

    Your numbers reversed:
    15
    1988563298
    10
    Some advice would be greatly appreciated :X

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Ah well, after reading some older threads, I changed some of the code:

    Code:
    #include <stdio.h>
    
    double main()
    
    {
    
    // Local Declarations
        double a, b, c;
    
    // Statements
        printf("Please enter three numbers: \n");
        scanf("%lf %lf %lf", &a, &b, &c);
    
        printf("\n\nYour numbers forward: \n%f \n%f \n%f\n\n", a, b, c);
        printf("Your numbers reversed: \n%f \n%f \n%f", c, b, a);
    
        return 0;
    
    }
    And I get
    Please enter three numbers:
    1 2 3


    Your numbers forward:
    1.000000
    2.000000
    3.000000

    Your numbers reversed:
    3.000000
    2.000000
    1.000000
    Any way I could make the output set the decimal to the hundredths place? o.o
    Last edited by absoluteZero; 04-17-2011 at 11:58 PM.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do you mean you want
    Code:
    Your numbers reversed:
    3.00
    2.00
    1.00
    ?

    If so, the way to do that is
    Code:
    printf("Your numbers reversed: \n%.2f \n%.2f \n%.2f", c, b, a);
    ; the twos are the number of decimal places you want
    absoluteZero likes this.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by TheBigH View Post
    Do you mean you want
    Code:
    Your numbers reversed:
    3.00
    2.00
    1.00
    ?

    If so, the way to do that is
    Code:
    printf("Your numbers reversed: \n%.2f \n%.2f \n%.2f", c, b, a);
    ; the twos are the number of decimal places you want
    Haha, I was just reading my book and I found out what to do >_>

    But thanks for the verification!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Newby - help with type double
    By talkingtina in forum C Programming
    Replies: 5
    Last Post: 04-13-2008, 11:23 PM
  2. Double type
    By Gordon in forum Windows Programming
    Replies: 7
    Last Post: 09-28-2007, 05:33 AM
  3. Double Data Type
    By HallmanBilly in forum C Programming
    Replies: 2
    Last Post: 09-01-2007, 03:25 PM
  4. std::accumulate with double type
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 12:23 AM
  5. Rounding & DOUBLE type
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-17-2002, 05:29 PM

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