whats going on here? (float question)

This is a discussion on whats going on here? (float question) within the C Programming forums, part of the General Programming Boards category; I have this simple program...that takes 4 integers and prints out the average Code: #include <stdio.h> int AVERAGE(int num1, int ...

  1. #1
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33

    whats going on here? (float question)

    I have this simple program...that takes 4 integers and prints out the average


    Code:
    #include <stdio.h>
    
    
    int AVERAGE(int num1, int num2, int num3, int num4);
    float OUTPUT(float output);
    
    int main(void){
    
    int a,b,c,d,x, avr, out;
    
    printf("Please enter four integers : ");
    scanf("%d%d%d%d",&a,&b,&c,&d );
    
    avr=AVERAGE(a,b,c,d);
    
    
    OUTPUT(avr);
    
    exit(0);
    }
    
    int AVERAGE(int num1, int num2, int num3, int num4){
    float result;
    result = (num1 + num2+ num3 + num4)/4;
    return(result);
    }
    
    float OUTPUT(float output){
    printf("Average of the 4 integers is %5.2f \n", output);
    }

    But for some reason i'm losing decimals. If for example i input 1, 2,3 and 4

    out put should be 2.5 but i get 2

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your AVERAGE function is returning an int, you probably want it to return a float.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    nope..that did not help

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This worked for me:
    Code:
    #include <stdio.h>
    
    float Average(int Num1, int Num2, int Num3, int Num4)
    {
    	float Result = Num1+Num2+Num3+Num4;
    	Result = Result/4;
    	return Result;
    }
    
    int main(void)
    {
    	printf("%f\n", Average(1,2,3,4));
    	return 0;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    Code:
    #include <stdio.h>
    
    
    float AVERAGE(int num1, int num2, int num3, int num4);
    float OUTPUT(float output);
    
    int main(void){
    
    int a,b,c,d,x, avr, out;
    
    printf("Please enter four integers : ");
    scanf("%d%d%d%d",&a,&b,&c,&d );
    
    avr=AVERAGE(a,b,c,d);
    
    
    OUTPUT(avr);
    
    exit(0);
    }
    
    float AVERAGE(int num1, int num2, int num3, int num4){
    float result;
    result = (num1 + num2+ num3 + num4);
    result = result /4;
    return(result);
    }
    
    float OUTPUT(float output){
    printf("Average of the 4 integers is %5.2f \n", output);
    }

    i did a few changes and nothing is helping

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Solution is simple. You were doing integer division. Do floating point division (done when you have at least one floating point operand). Also, return a float.
    Code:
    float AVERAGE(int num1, int num2, int num3, int num4){
    float result;
    result = (num1 + num2+ num3 + num4)/4.0;
    return(result);
    }
    Now with my coding style :
    Code:
    float average(int num1, int num2, int num3, int num4)
    {
       return (num1 + num2+ num3 + num4) / 4.0;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Make 'avr' a float.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    Originally posted by Dave_Sinkula
    Make 'avr' a float.
    thanks..that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array and classes problem
    By armen in forum C++ Programming
    Replies: 9
    Last Post: 11-21-2008, 09:32 PM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:17 AM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  5. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 01:47 AM

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