Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coding Help with Arrays

    Code:
    public class ArrayMethods
    {
      private double[] a;
      
      public static double sumOfArrays(double[] a)
      {
        double max = a[0];
        
        for(int i =1; i<a.length; i++)
        {
          if(a[i] > max)
            max = a[i];
        }
        
        double min = a[1];
        for(int i =1; i<a.length; i++)
        {
          if(a[i] < min)
            min = a[i];
        }
        
        double difference = max - min;
        return difference;
      }
      
      public static void main(String[] args)
      {
        double [] b = {43.9,12.3,99.6,48.2,2.1,65.5};
        double diff = differenceOfArrays(b);
        System.out.println(diff);
      }
    }
    When I run the code, at the line where I run the method I wrote, double diff = differenceOfArrays(b);, it says it cannot find the symbol. Before I wrote it, I didn't have the method as static and when I didn't have static there it said that it couldn't be dereferenced. So I made it static. Could anyone help with this? It would be greatly appreciated.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have no method called differenceOfArrays. All you have here is sumOfArrays, although given what it does I'd suspect it should have its signature changed to differenceOfArrays.
    An easier solution btw would be to first sort the array from Arrays.sort, then pull both the first and last elements in the array, and calculate the difference. Otherwise, you can perform both the max and the min within the same loop by evaluating both min and max to the current iteration to see if it should be bumped.


  •  

    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
    •