1

I wrote this:

    public static decimal Average(int a, int b)
    {
        return (a + b) / 2;
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(Average(2, 1));
        Console.ReadKey();
    }

but it returns 1 . But it should return 1.5 How can I fix it to return 1.5 ?

Bohn
  • 26,091
  • 61
  • 167
  • 254

3 Answers3

10

Missing typecasting on the Average function

public static decimal Average(int a, int b)
{
    return (decimal)(a + b) / 2;
}

public static void Main(string[] args)
{
    Console.WriteLine(Average(2, 1));

}
shat90
  • 405
  • 3
  • 8
  • 6
    This is right, but to explain WHY it's right: In integer division, 3 / 2 is 1. In floating-point division, 3/2 is 1.5. Your return type is decimal which is a floating-point type, but your operands are all integers, so what happens is that it first performs integer math, yielding 1, and then converts to decimal. Instead, you need to convert at least one of the operands to decimal first and then do the division. – PMV Oct 21 '16 at 02:32
  • return (decimal)(a + b) / 2; should be return (float)(a + b) / 2; – Ashish Sep 04 '18 at 11:07
1

It returns 1 as integers do not have decimal points and hence the answer is truncated. Eg if the result was 1.99 the result would be 1, eg floor(result).

If you require decimal points you need to use floats or cast your integers to floats. If you require higher precision a double precision floating point could be used (double).

Something else to consider is there are libraries that will do this for you. Eg List.Average() which will average multiple variables.

edit: See this question for a more detailed answer that is specific to division

What is the behavior of integer division?

Community
  • 1
  • 1
rollsch
  • 2,518
  • 4
  • 39
  • 65
1

Solutions upper are wrong. You must check next test cases when work with types:

TEST CASE

0 100

20 -21

21 20

2147483647 2147483647

-2147483647 2147483647

-2147483647 -2147483647

#include <stdio.h>
#include <limits.h>


int average(int a, int b) {
    if ( a < 0 && b < 0 ) {
        b -= a;
        b /= 2;
        b += a;
    } else if ( a < 0 ) {
        b += a;
        b /= 2;
    } else if ( b < 0 ) {
        b += a;
        b /= 2;
    } else if ( a > b ) {
        a -= b;
        a /= 2;
        a += b;
    } else {
        b -= a;
        b /= 2;
        b += a;
    }
    
    return b;
}


int main(){
    int a = INT_MAX;
    int b = INT_MAX;

    scanf("%d %d", &a, &b);

    printf("Max INTEGER: %d\n", INT_MAX);

    printf("Avarage INTEGER: %d\n", average(a, b));

    return 0;
}
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Emmanuel Ponnudurai Sep 04 '21 at 04:21