Average of an array rounding problem

Back to Problem Solutions forum

transingularity     2015-06-27 20:15:28

Hi, I'm trying to make average of an array problem. Actually I made it and I get answers. But rounding doesn't work. It always round to less integer. I use Math.round(). Even if one number is 120.95947, it rounds it to 120. Can somebody explain what I did wrong? Thank you.

And I used here Strings, I guess it could be easier to make code without using string. But that's how I could make it. Sorry.

import java.util.*;

public class Solution {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int col = in.nextInt();
    in.nextLine();
    for (int i = 0; i < col; i++ ) {
        String str = in.nextLine();
        String[] string;
        string = str.split(" ");
        int sum = 0;
        double avg = 0d;
        for (int j=0; j<string.length-1; j++) {
            sum = sum + Integer.parseInt(string[j]);
        }
        avg = ((sum/(string.length - 1)));

        System.out.print((int)Math.round(avg) + " ");
    }
}
}
ugsdhfjbnsja     2015-06-27 21:11:31

Just had a quick look over it. Using Strings should still work, it is not the most efficient but you can sort this after you get correct answers (EDIT: Based on how the problem is set, where you need to read full lines, using Strings probably is the best way to go actually, I wouldn't really worry about changing any of it). The only thing I have noticed which you may want to change is your second for loop.

I think you do not want the -1, otherwise you never look at the last element of the array.

 for(int j=0; j<string.length; j++){

I am continuing to look over it at the moment - I haven't got to that problem yet myself so am unfamiliar with it.

I'll post back if I find the cause of your issues - the -1 isn't the issue you are asking about but I think it is still causing issues.

ugsdhfjbnsja     2015-06-27 21:21:39

Ok, I think I fixed it. You will want to test it yourself though, I still haven't read the problem page.

I normally wouldn't do the solution for you but you were extremely close.

import java.util.*;

public class j {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int col = in.nextInt();
        in.nextLine();
        for (int i = 0; i < col; i++ ) {
            String str = in.nextLine();
            String[] string;
            string = str.split(" ");
            int sum = 0;
            double avg = 0d;
            for (int j=0; j < string.length; j++) {
                sum = sum + Integer.parseInt(string[j]);
            }
            double doubleSum = (double)sum;
            avg = (doubleSum/((double)string.length));
            System.out.print(Math.round(avg) + " ");
        }
    }
}

The biggest problem was integer division. You were trying to make a double using only integer variables, thus it was always rounded down and usign Math.round() therefore did nothing at all - it always appeared that Math.round() was what rounded it down for you, when it wasn't. Next time, add in some extra System.out.println() around the place to check what your variable values are coming out to (or use a debugger). That was how I saw that the division weren't workign correctly.

Hope I helped,

Mike

https://github.com/mikegreen1995

Guy Gervais     2015-06-27 21:23:44
User avatar

Your "sum" is an int. When you do "avg = sum / string.length", java sees double = int / int, so it does integer division and assigns the integer results to a double.

Use "double" for sum and it should help. Also, lose the "-1" at both places like Mike Green suggested, or your results will be off.

Guy Gervais     2015-06-27 21:24:39
User avatar

Looks like Mike beat me to it. :)

transingularity     2015-06-28 09:46:30

Thank you, guys. The mistake was in Integer "sum". I changed it and everything works good. Well. About "-1" in

avg = ((sum/(string.length - 1)));

That means that I don't want to count last zero from array. Becasue

Input data will give the number of test-cases in the first line.
Then test-cases themselves will follow, one case per line.
Each test-case describes an array of positive integers with value of 0 marking end.
(this zero should not be included into calculations!!!).

Thank you very much for help. :)

kanat.s13     2015-12-08 08:55:47

thanks for you solution! I don't know about split the line.

Please login and solve 5 problems to be able to post at forum