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.
cont...
If the largest number was in the last spot it would make sense because it keeps running till the end of the loop and as long as the if statement is true but in some scenarios that's not the case. Additionally would I just do the reverse to get the smallest?
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.
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
cont...
the last position, which is represented by int c.
2. I have ar[c], c increases by 1 each time, starting from 0.
3. Then I have if(ar[c]>a) a=0
4. Then I have a=ar[c]. Which gives me the largest int but I don't understand how. How does it know to print out the largest if the code just has it running through the whole array? Thanks
Basically it declares a variable (c in your code) that will always be equal to the largest number of all the numbers seen so far. How does it ensure this? Since if a bigger number is encountered, c is immediately set to it. Once it's gone through all the numbers, it knows it's set to the largest value. (If it ever passed a larger number, it would have been set to it.)
(It's important to realize that the loop goes through all the numbers., since it was set to only stop once it reached the end of the array.)
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?
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);
cont...
Then I still don't understand how it knows what the largest number is. My loop just goes through the array one spot at a time. And simultaneously largest changes as the loop goes through the array.
@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.
@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:
cont...
The code to find the largest number:
int[] ar = {4,5,3,6,1};
int largest=ar[0];
for(int n: ar){ //goes through each number in order
if(n>largest){
largest=n;
}
}
largest is set to 4 in the second line of code.
Let's go through each iteration of the loop.
@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
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;
Comments
Bernard Mitchell
Jul 15, 3:30 PMim not sure on how to pull out the least and the greatest from the array. I think I have the setup correct.
Learneroo
Jul 15, 3:36 PMWhat did you try so far? You should be able to solve it based off example discussed earlier.
Bernard Mitchell
Jul 15, 4:32 PMOkay 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.
If the largest number was in the last spot it would make sense because it keeps running till the end of the loop and as long as the if statement is true but in some scenarios that's not the case. Additionally would I just do the reverse to get the smallest?
Learneroo
Jul 15, 5:36 PMNot 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
Jul 16, 3:14 PMOkay , 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
the last position, which is represented by int c.
2. I have ar[c], c increases by 1 each time, starting from 0.
3. Then I have if(ar[c]>a) a=0
4. Then I have a=ar[c]. Which gives me the largest int but I don't understand how. How does it know to print out the largest if the code just has it running through the whole array? Thanks
Learneroo
Jul 16, 3:41 PMThat'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.
Basically it declares a variable (
c
in your code) that will always be equal to the largest number of all the numbers seen so far. How does it ensure this? Since if a bigger number is encountered,c
is immediately set to it. Once it's gone through all the numbers, it knows it's set to the largest value. (If it ever passed a larger number, it would have been set to it.)(It's important to realize that the loop goes through all the numbers., since it was set to only stop once it reached the end of the array.)
Bernard Mitchell
Jul 17, 11:30 AMOkay 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
Jul 17, 11:33 AMYou 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
Jul 17, 11:48 AMint 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);
Then I still don't understand how it knows what the largest number is. My loop just goes through the array one spot at a time. And simultaneously largest changes as the loop goes through the array.
thales
Jul 18, 1:09 PMIn 2 short lines, the solution|:
my code
Bernard Mitchell
Jul 20, 2:41 PM@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
Jul 20, 3:20 PM@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:
The code to find the largest number:
largest
is set to 4 in the second line of code.Let's go through each iteration of the loop.
so now that the loop is done,
largest
is 6, the largest number in the array.Bernard Mitchell
Jul 20, 3:56 PM@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
Jul 20, 3:59 PMThanks I understand now!!!! ;)
Kami
Mar 7, 8:16 AMSystem.out.println(sum);
}