# Big and Small

Premium Content - Free Preview

This challenge reviews finding the largest and smallest numbers in an array.

• #### Bernard Mitchell

im not sure on how to pull out the least and the greatest from the array. I think I have the setup correct.

• #### Learneroo

What did you try so far? You should be able to solve it based off example discussed earlier.

• #### Bernard Mitchell

Okay I understand how to write the code but I don't understand how it works. The if statement keeps going as long as int c is greater than zero. I still don't get how it picks the largest number.

• #### Learneroo

Not sure what you're asking, maybe you can link to the code? When you're not sure what would happen, try walking through the code (with a specific input) with a pencil to keep track of the values at each line. You can also try the Visual Debugger.

• #### Bernard Mitchell

Okay , I will try. I'm just confused on how the code detects the largest integer. I'll try and break it down step by step.
1. In my code the loop runs through the array from the first position(0)through

• #### Learneroo

That's the basic algorithm given as an example in about programming and the following page. The Java code example is discussed in Arrays and Loops.

• #### Bernard Mitchell

Okay I guess it's just the way Java operates. I didn't know that it would automatically be able to pick the largest number out of an array. Do the words "largest" and smallest play a role or can the int be defined with anything?

• #### Learneroo

You can name your variables anything. Variable names never do anything, they're just named for clarity. The loop is what finds the numbers.

• #### Bernard Mitchell

int largest=0;
int b=ar.length-1;
for(int c=0;c<=b;c=c+1){
if(ar[c]>largest){
largest=ar[c];
}
}
System.out.println(largest);

• #### thales

In 2 short lines, the solution|:
my code

• #### Bernard Mitchell

@thales I you're more advanced than me, can you answer my question in the previous comment. Since @Leaneroo doesn't understand or hasn't gotten around to responding.

• #### Learneroo

@Bernard, I emailed you a long reply 3 days ago. It's copied below, edited to use a for-each loop.

It seems you're trying to understand how the algorithm gets the largest number in an array. Please see the links above that discuss this algorithm. Here is the algorithm step-by-step:

• #### Bernard Mitchell

@Learneroo thank you for the comment , I didn't receive your email with the long explanation. My apologies if my comment sounded like a slight. I appreciate all the help. Cheers

• #### Bernard Mitchell

Thanks I understand now!!!! ;)

• #### Kami

``````int largest = ar[0];
int smalest = ar[0];
int sum=0;
for(int i=0; i < ar.length; i++){
if(ar[i] > largest){
largest = ar[i];
}
if(ar[i]< smalest){
smalest= ar[i];
}
}
sum=largest-smalest;
``````

System.out.println(sum);
}