value of max

Posted on 2005-05-08
Last Modified: 2010-04-17
I am tryaing to Write a method to compute and return the value of max – min where max is the largest element of an int array and min is the smallest element of an int array.  The method is passed the int array and an int value that denotes the number of elements in the array.  You may assume that the int array has at least 1 element in it.

I want some body how know java very well to tell me if it is right or wrong

public int compute(int d[])
  int size = d.length;
  int min=0,max=0;
  max = d[0];
  for(int i=0;i<size;i++)
     min = d[i];
     max = d[i];  
 return max – min;
Question by:Ansary100
    LVL 21

    Accepted Solution

    The only mistake i see is that you entered min=data[0] and I think you meant min=d[0].  You can also replace
      int min=0,max=0;
      max = d[0];
      int min,max;
    But that is just shorthand and does not effect the overall speed of the routine.

    Since you have already processed the 0th item, you can also start the for(...) loop with 1:
      for(int i=1;i<size;i++)
    LVL 3

    Expert Comment


    I wonder if you could dispense with initializing min and max to zero, since you re-initialize them right away to d[0].  In other words:

    int size = d.length;
    int min = d[0];
    int max = d[0];


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    withoutString  challenge 40 119
    linearIn  challenge 23 54
    Fibonacci challenge 11 61
    pairs challenge 5 29
    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now