Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By Ronin

Thread: Calculating average of two arrays - Calculation coming out incorrect

  1. #1
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Calculating average of two arrays - Calculation coming out incorrect

    I am working on an assignment, and I have come up with a program that runs, but it does not run correctly. This is the assignment:

    Write two overloaded methods that return the average of an array with the following headers:
    public static int average(int[] array)
    public static double average(double[] array)
    Write a test program that prompts the user to enter ten double values, invokes this method and displays the average value.

    When I run it my averages are coming out incorrect, and I am not sure why. I have tried moving and rearranging things and nothing is working. I appreciate any help/advice.
    Java Code:
       
        public static int average(int[] array) {
            int sum = 0;
            int average = 0;
            for (int i = 0; i < array.length; i++) {
                sum = sum + array[i];
            }
            average = sum / array.length;
            return average;
    
        }
    
        public static double average(double[] array) {
            double sum = 0;
            double average = 0;
            for (int i = 0; i < 5; i++) {
                sum = sum + array[i];
            }
            average = sum / array.length;
            return average;
        }
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter Integer values: ");
            int array1[] = new int[4];
            for (int i = 0; i < array1.length; i++) {
                array1[i] = input.nextInt();
            }
            int avg1 = average(array1);
            System.out.println("Average of First Array: " + avg1);
            System.out.println();
            
            System.out.print("Enter Ten Double Values: ");
            double array2[] = new double[10];
            for (int i = 0; i < array1.length; i++) {
                array2[i] = input.nextDouble();
            }
            double avg2 = average(array2);
            System.out.println("Average of Second Array: " + avg2);
    
        }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    make float instead of int ..ur code is correct only.
    Ramya:cool:

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    10

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    I would have to differ. Check line 15.

    Regards.
    DarrylBurke and JosAH like this.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    I think you misread the assignment:

    "Write two overloaded methods that return the average of an array with the following headers:
    public static int average(int[] array)
    public static double average(double[] array)"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    What is wrong with it? I am not noticing anything. Is it the sum = sum part you are talking about? I am new to this

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    Check the number of elements in that array ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Re: Calculating average of two arrays - Calculation coming out incorrect

    Thank you I think I fixed it, plus I was not think the first one is only an int. So, if it is not even it will not show the exact number. Thank you again

Similar Threads

  1. java Calculating the mean average
    By danielki in forum New To Java
    Replies: 5
    Last Post: 02-22-2014, 03:39 AM
  2. Calculating Average using Arrays....help!
    By knightwriter in forum New To Java
    Replies: 5
    Last Post: 12-06-2011, 10:02 PM
  3. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 05:32 AM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  5. Replies: 1
    Last Post: 03-11-2010, 05:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •