Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default average of an array

    Hi. I'm having trouble finding the average of an array. I was wondering if you could help me.

    Error:

    Average.java:7: error: '.class' expected
    a += A[ ] / A.length;
    ^( under / )
    1 error


    What I'm asked to do:

    Write a function called Average( ) that takes as a parameter a one dimensional array of doubles called A.
    Double Average( double A[ ] )
    The function computes and returns the average value of the elements in the array (the sum of the elements in the array divided by the number of elements).


    Average:
    Java Code:
    public class Average {
        public Average() {}
        
            double Average( double A[ ] ) {
                double a = 0;
                
                   a += A[ ] / A.length;
                return a;
            }
    }

    AverageTest:
    Java Code:
    public class AverageTest {
        public static void main( String [] args ) {
    	Average a = new Average( );
                    
               double total;
               
               double [] A = { 87.5, 68.1, 94.6, 100.7, 83.9, 78.3 };
               
               for ( int counter = 0; counter < A.length; counter++ )
               double total = a.Average(a);
               
               System.out.println("Average of the array is " + total = ".");
        }             
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: average of an array

    You seem to be misunderstanding how an average is calculated. First you add all values up and then divide once at the end. Also what is the empty brackets for?

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: average of an array

    Is there a particular reason why you declaring an object called Average? Move the Average function out of the Average class and past it under the main function. You want to have the for loop inside the average function.

    For each value in the array
    update total
    end for

    return total

    Also, your functions are meant to start with a lower case letter and classes with Upper case. As you can see when I say Average, you may not realise what I mean - the function or the class, so follow Java standard.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: average of an array

    I don't know if I'm suppose to use " Double Average( double A[ ] ) " as it is in the problem.
    And I don't know what the empty brackets for.

    This is my first time in a java class and first time working with arrays.
    I know its all wrong but I'm trying to learn since I majoring in computer science and engineering.
    Also do you know where is a really good website that I can use as practice and learn more. Something that is easy to understand.

    And I do know what average is... add up all the numbers and divide it by the total of numbers.

    I changed my code but its still wrong

    Java Code:
    public class Average {
        public Average() {}
        
            double Mean( double A[ ] ) {
                double a = 0;
    	    double m = 0;
    
    		a += A[ ];
    		m = a / A.lenght;
                return m;
            }
    }


    Java Code:
    public class AverageTest {
        public static void main( String [] args ) {
    	Mean m = new Mean( );
                    
               double total;
               
               double [] A = { 87.5, 68.1, 94.6, 100.7, 83.9, 78.3 };
               
               for ( int counter = 0; counter < A.length; counter++ )
               double total = m.Mean(a);
               
               System.out.println("Average of the array is " + total = ".");
        }             
    }

    Thank you for you're help.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: average of an array

    The reason I asked about the empty brackets was in the hope that you would think about what the correct syntax is. You need to place a value inside the brackets to indicate what index of the array you are trying to access.
    Java Code:
    int[] arr = {12,5,23};
    int x = arr[0];
    This will assign the value 12 to the variable x as the value 12 is stored in the array with an index of 0.

    OK so you know how to calculate the average. Writing it in code is not that different. So doing it in your head you would add the first number to a total, then the second, then the third the the fourth etc. Do you see a pattern? What type of statement do you think you need to achieve adding numbers over and over again?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: average of an array

    Java Code:
    Mean m = new Mean( );
    Looks like you are just guessing now. You need to clear your mind and think clearly not take wild stabs at it.

Similar Threads

  1. more accurate average of array data
    By jwl in forum New To Java
    Replies: 2
    Last Post: 10-28-2012, 10:14 PM
  2. Random Array with average
    By keki in forum New To Java
    Replies: 7
    Last Post: 04-24-2012, 05:14 PM
  3. Average of an array. Please help
    By vika in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 09:09 PM
  4. 2 dimensional array, average student
    By aborgeld in forum Advanced Java
    Replies: 5
    Last Post: 03-05-2011, 03:12 PM
  5. Can't get correct average using array
    By nevets93 in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 10:33 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
  •