Thread: Double precision number help

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Double precision number help

    I am new to C Programming and have been doing well so far. I am currently hitting a snag with "double" ie. double precision numbers. The code below is currently working correctly with int integer inputs and I'm trying to get it to instead take in double precision ones and still compile correctly. Help would be great

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        #define Maxgrade 3
        int grade [Maxgrade];
        int average, total;
        int i;
        
        
        total=0.0;
        
        for (i=0; i<Maxgrade; i++){
            printf ("Enter a grade:  ");
            scanf ("%d", &grade[i]);
            }
            
        for (i=0; i<Maxgrade; i++)
        {
            total += grade[i];
              }
        printf ("\nThe total is %d\n", total);
        
        average = total/3;
        
        printf ("\nThe average is %d\n\n\n", average);
            
        for (i=0; i<Maxgrade; i++){    
           if (grade[i]<average)
           printf ("*");
           
           if (grade[i]>=90)
           printf ("%d, A\n", grade[i]);
        
           else if (grade[i]<90, grade[i]>=80)
           printf ("%d, B\n", grade[i]);
        
           else if (grade[i]<80, grade[i]>=70)
           printf ("%d, C\n", grade[i]);
        
           else if (grade[i]<70, grade[i]>=60)
           printf ("%d, D\n", grade[i]);
        
           else if (grade[i]<60)
           printf ("%d, F\n", grade[i]);
            }
            
        system ("PAUSE");
        return 0;
        
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    To input a double use "%lf"; to print a double use "%f"

    Code:
    /* ... */
    double grade[MAXGRADE]; /* Use ALLCAPS for define'd constants */
    /* ... */
    scanf("%lf", &grade[i]);
    /* ... */
    printf("%f", grade[i]);
    /* ... */

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Well, for one thing, you need to declare grade, average, and total as double types.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    It worked

    Quote Originally Posted by qny View Post
    To input a double use "%lf"; to print a double use "%f"

    Code:
    /* ... */
    double grade[MAXGRADE]; /* Use ALLCAPS for define'd constants */
    /* ... */
    scanf("%lf", &grade[i]);
    /* ... */
    printf("%f", grade[i]);
    /* ... */

    Thanks a ton, it was the "%lf" scanf that was holding me up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double precision is not correct
    By Ilyas Patanam in forum C Programming
    Replies: 5
    Last Post: 06-24-2011, 01:25 AM
  2. Manipulating individual bits in a double precision number
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2010, 07:19 AM
  3. numerical precision of a double
    By mc61 in forum C Programming
    Replies: 6
    Last Post: 03-11-2008, 12:23 PM
  4. streams of double and precision
    By zeb in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2008, 06:11 AM
  5. More precision than a double
    By manutd in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2006, 06:49 AM

Tags for this Thread