Thread: cant get a double type output ??

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    cant get a double type output ??

    cant get a double type output ??-programming-exercises-7-2-_page1111111111111_2-jpg

    this is what I got so far
    Code:
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int i=0, j,max,min=10000;
        float sum,sum1,sum2;
    
    
        int studentGrades[4][3];
    
    
        max=i;
    
    
        printf("Enter all grades for each student respectively : \n");
    
    
        for(i=0; i<4; i++){
            for(j=0; j<3; j++){
            scanf("%d",&studentGrades[i][j]);
                if (max < studentGrades[i][j])
                 max = studentGrades[i][j];
            else if (min > studentGrades[i][j])
                min = studentGrades[i][j];
        }
        }
    
    
        printf("\nThe array is:\n\n");
        printf("\t\t   [0] [1] [2] [3]");
    
    
        for(i=0;i<3; i++){
            printf("\nStudentgrades [%d]", i);
            for(j=0;j<4; j++){
    
    
                printf("%4d",studentGrades[i][j]);
    
    
            }
    
    
            }
    
    
    
    
            printf ("\n\n\n\nHighest grade: %d\nLowest grade: %d", max, min);
    
    
    
    
            sum=(studentGrades[0][0]+studentGrades[0][1]+studentGrades[0][2]+studentGrades[0][3])/4;
            sum1=(studentGrades[1][0]+studentGrades[1][1]+studentGrades[1][2]+studentGrades[1][3])/4;
            sum2=(studentGrades[2][0]+studentGrades[2][1]+studentGrades[2][2]+studentGrades[2][3])/4;
    
    
    
    
            printf("\n\nThe average grade for students [0] is %.2f",sum);
            printf("\n\nThe average grade for students [1] is %.2f",sum1);
            printf("\n\nThe average grade for students [2] is %.2f",sum2);
    
    
            printf("\n\n\n\n");
    
    
    
    
        return 0;
    }
    the problem is that when I print out the three sums at the end of the program I dont get any decimal points but just zeros like something.00 instead of something.50 etc

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by hummingdev View Post
    the problem is that when I print out the three sums at the end of the program I dont get any decimal points but just zeros like something.00 instead of something.50 etc
    Well, studentGrades is an array of ints, and 4 is an int. Thus, when you divide them (what we refer to as integer division), the type of the result is int, and can't store the fractional component. Note that the type of the result is not the same as the type of the variable you assign this result to.

    You need to force the compiler to treat it as floating point division. Either cast one of the operands to type float
    Code:
    sum = (blah + blah + blah) / ((float) 4);
    or divide by 4.0 (which forces the compiler to treat the denominator as a float).
    Code:
    sum = (blah + blah + blah) / 4.0;
    [EDIT]
    Note, neither of those are quite ideal, see list below, especially #1 and 4.
    [/EDIT]

    A few more notes/complaints:

    1. Don't use magic numbers. You should #define constants #define NUM_STUDENTS 3 and #define NUM_GRADES 4. Use the named constants in all the appropriate places (including the division).
    2. Whenever you see yourself numbering variables like sum, sum1, sum2, you should look into using arrays. You should do that in your program.
    3. sum is actually a bad name. It's not a sum, it's an average (specifically mean average). Name your variables appropriately -- average would be good.
    4. You can use a loop to add up the grades for each student and to print them out. That way, if you change NUM_STUDENTS or NUM_GRADES, you don't need to change anything else, it will just work.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    @anduril462
    thanks man , you explained it brilliantly

    just can you please clarify on number 4. .. how do I add up the grades for each student when it's 2D array ?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    With a for loop. Look at the line for sum
    Code:
    sum=(studentGrades[0][0]+studentGrades[0][1]+studentGrades[0][2]+studentGrades[0][3])/4;
    What changes each time? So your loop index/counter variable might go where? Make sure you initialize sum to a good value.

    Note, you may want to use two nested for loops, one to iterate through each student, one for each grade for a given student. Similar to how you read in the values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  2. 'Type double' help D:
    By absoluteZero in forum C Programming
    Replies: 3
    Last Post: 04-18-2011, 12:17 AM
  3. Double type
    By Gordon in forum Windows Programming
    Replies: 7
    Last Post: 09-28-2007, 05:33 AM
  4. Double Data Type
    By HallmanBilly in forum C Programming
    Replies: 2
    Last Post: 09-01-2007, 03:25 PM
  5. Rounding & DOUBLE type
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-17-2002, 05:29 PM