Results 1 to 7 of 7
 10082014, 07:12 AM #1Member
 Join Date
 Sep 2014
 Posts
 10
 Rep Power
 0
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); } }
 10082014, 09:53 AM #2
Re: Calculating average of two arrays  Calculation coming out incorrect
make float instead of int ..ur code is correct only.
Ramya:cool:
 10082014, 11:43 AM #3Senior Member
 Join Date
 Oct 2010
 Posts
 393
 Rep Power
 10
Re: Calculating average of two arrays  Calculation coming out incorrect
I would have to differ. Check line 15.
Regards.
 10082014, 04:18 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10082014, 08:05 PM #5Member
 Join Date
 Sep 2014
 Posts
 10
 Rep Power
 0
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
 10082014, 08:07 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Re: Calculating average of two arrays  Calculation coming out incorrect
Check the number of elements in that array ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10082014, 09:47 PM #7Member
 Join Date
 Sep 2014
 Posts
 10
 Rep Power
 0
Similar Threads

java Calculating the mean average
By danielki in forum New To JavaReplies: 5Last Post: 02222014, 03:39 AM 
Calculating Average using Arrays....help!
By knightwriter in forum New To JavaReplies: 5Last Post: 12062011, 10:02 PM 
finding average in 2d arrays
By the beginner in forum New To JavaReplies: 18Last Post: 02242011, 05:32 AM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 06:03 AM 
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03112010, 05:21 AM
Bookmarks