Finding the Average of all Odd and Even Numbers from an Input/Text File

4.5k views Asked by At

Like the title states I have been looking to create a program that will loop over a text/input file (of numbers), then go on to find the Average for both odd and even numbers present in that file.

I have currently managed to create what is shown below which has resulted in being able to find the Average for the Odd Numbers. However I can't seem to get my head around being able to get the Average for the Even Numbers though.

I tried closing the scanner, opening up another scanner and making another while, if else loop like below and pretty much tried the opposite, other attempts to try and obtain the Even Average have ended up with the output being NaN, 0 or stuck on 26 at times (the 26 is probably down to my input file used but the math/s didn't make sense for obtaining the even average when I worked it out by hand)

This has been bothering me for some time now and was wonder if anyone could point me in the right direction on how I could obtain the Even Average as well as what I currently have.

Scanner scan = new Scanner(AvgOddEven.class.getResourceAsStream("Numbers.txt"));

int count = 0, sum = 0, evenCount = 0, oddCount = 0;

while (scan.hasNextInt()) {
    int number = scan.nextInt();
    if (number % 2 == 0) // even numbers are detected here
        evenCount++; //those detected are counted here.
    else
        oddCount++;
    sum += number;
    count++; //Counts all numbers in the text file
}

System.out.println("Sum divided by odd = average: " + sum / oddCount); //This gets me the average of odd numbers

Below is the text file I used. (Apologies for it being quite long)

1
5
17
16
10
7
11
39
1
15
13
2

There are 2 answers

0
ryanyuyu On BEST ANSWER

I don't think your code does what you think it does. No matter whether you number is odd or even, you are always adding to the sum. So in fact your current code is currently dividing sum/oddCount which the sum of all numbers divided by the number (count) of odd numbers.

To fix this and implement the average of even numbers, you should keep two separate variables for the evenSum and oddSum.

Your javascript modified would like something like this:

...
int count = 0, evenSum = 0, evenCount = 0;
double oddSum = 0.0, oddCount = 0.0; //for floating point arithmetic later

while (scan.hasNextInt()) {
  int number = scan.nextInt();
  if (number % 2 == 0) { // even numbers are detected here
    evenSum += number;
    evenCount++; //those detected are counted here.
  }
  else {
    oddSum += number;
    oddCount++;
  }
  count++; //Counts all numbers in the text file
}

System.out.println("Average of odds: " + oddSum / oddCount);
System.out.println("Average of evens: " + evenSum / evenCount);

One final thing to be aware of is the difference between integer division and floating point division. Since you've declared all your variable as ints, your average will perform integer division, which truncates any decimal portion. To perform floating point division, at least one of your operands must be a floating point type. I've handled that by changing the type of the sum variables to a floating point type (a Double or double-precision float).

3
René Roth On

Your code has some logical problems. You're calculating the sum for all numbers, no matter if even or odd, and then trying to get an average for only a subset of the number, which won't work.

You will need to calculate different sums for even and odd numbers:

Scanner scan = new Scanner(AvgOddEven.class.getResourceAsStream("Numbers.txt"));

int count = 0, sumEven = 0, sumOdd = 0, evenCount = 0, oddCount = 0;

while (scan.hasNextInt()) {
  int number = scan.nextInt();
  if (number % 2 == 0) { // even numbers are detected here
    evenCount++; //those detected are counted here.
    sumEven+=number;
  }
  else {
    sumOdd+=number;
    oddCount++;
  }
  count++; //Counts all numbers in the text file
}

System.out.println("Average of odd numbers: " + sumOdd / oddCount);
System.out.println("Average of even numbers: " + sumEven / evenCount);