Solved

# value of max

Posted on 2005-05-08
277 Views
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;
min=data[0];
max = d[0];
for(int i=0;i<size;i++)
{
if(d[i]<min)
min = d[i];
if(d[i]>max)
max = d[i];
}
return max – min;
}
0
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;
min=data[0];
max = d[0];
with
int min,max;
min=max=d[0];
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++)
0

LVL 3

Expert Comment

Ansary100,

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];
etc.

mathbiol
0

## Featured Post

### Suggested Solutions

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…

#### Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!