Thread: program cannot compute decimal point?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    33

    Question program cannot compute decimal point?

    i want to average get the average of 3 grades.
    formula grade1+grade2+grade3/300*50+50
    for example 90+90+90/300 = 0.9 * 50 + 50 = 95 (calculator)
    but when the program divides i get total/300 = 0.0000 *50x50 = 50.00000 instead of 95
    Code:
    #include<stdio.h>                 //important since printf#include<conio.h>
    main()                                                                                      
    {
        clrscr();
        int Grade1,Grade2,Grade3,total;
        float result,average;
        printf("\nInput Grade1 out of 100:");
        scanf("%d",&Grade1);
        printf("\nInput Grade2 out of 100:");
        scanf("%d",&Grade2);
        printf("\nInput Grade3 out of 100:");
        scanf("%d",&Grade3);
        total = Grade1+Grade2+Grade3;
        printf("\nYour total grade is:""%d",total);
        average = total/300;
        printf("average is ""%f",average);
        result = average*50+50;
        printf("\nResult:""%f",result);
        getch();
    }
    Last edited by langamer101; 12-06-2011 at 12:42 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Make total a float.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    thanks tater, I was surprised that i just had to make total a float.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by langamer101 View Post
    thanks tater, I was surprised that i just had to make total a float.
    With total as an integer, the decimal points were truncated in line 15. The right side of the equation would be calculated as an integer then assigned to your floating point average. By the time it got to line 17, you already had the wrong answer.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Code:
        printf("\nYour total grade is:""%d",total);
        average = total/300;
        printf("average is ""%f",average);
        result = average*50+50;
        printf("\nResult:""%f",result);
       
    }
    May I ask the purpose of the two quotes before your modifiers?
    They're not needed.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FloridaJo View Post
    Code:
        printf("\nYour total grade is:""%d",total);
        average = total/300;
        printf("average is ""%f",average);
        result = average*50+50;
        printf("\nResult:""%f",result);
       
    }
    May I ask the purpose of the two quotes before your modifiers?
    They're not needed.
    Nice catch! I totally missed that.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    They're not needed, but they're perfectly valid. It is using what is known as compile-time string concatenation. The consecutive double quotes will both be omitted from the output.
    It's useful in a circumstance such as:
    Code:
    string board =
        " | | \n"
        "-+-+-\n"
        " | | \n"
        "-+-+-\n"
        " | | \n";
    This allows you to see how the multi-line text will appear at runtime, without resorting to run-time concatenation which is less efficient, e.g.:
    Code:
    string board;
    board += " | | \n";
    board += "-+-+-\n";
    board += " | | \n";
    board += "-+-+-\n";
    board += " | | \n";
    Or writing it as one long string which is less readable e.g.:
    Code:
    string board = " | | \n-+-+-\n | | \n-+-+-\n | | \n";
    Last edited by iMalc; 12-07-2011 at 12:42 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Technically true.
    But it looked more like a misunderstanding than a concatenation.
    That's why I asked.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Code:
    string board;
    board += " | | \n";
    board += "-+-+-\n";
    board += " | | \n";
    board += "-+-+-\n";
    board += " | | \n";
    You don't actually expect that to work in C, do you? Maybe in C++ ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a value Before/After a decimal point in C ??
    By ammar555 in forum C Programming
    Replies: 7
    Last Post: 05-18-2011, 01:47 PM
  2. how to remove zero after decimal point
    By abhay_m8 in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2007, 12:11 AM
  3. Changing decimal point
    By Ikon in forum C Programming
    Replies: 7
    Last Post: 03-01-2006, 06:36 PM
  4. Testing for a decimal point
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2002, 12:46 AM
  5. Replies: 3
    Last Post: 11-28-2001, 07:53 AM

Tags for this Thread