I'm kind of new to Java. I've written a simple code for finding and printing the minimal number in an array. It turns out that I can't print the number since one of the variables is initialized inside a loop. If I initialize it outside the loop it would print out the first number only. If I put the println statement inside the loop it prints all the numbers. Please look at the code below and if someone can help I'd be grateful. 10x
[ May 31, 2008: Message edited by: Nikolay Tsonkov ]
Welcome to JavaRanch. As a small note, when posting code, it is much easier to read if you place it inside UBB [code] [/code] tags. And the easier it is to read the code, the easier it is for people to assist you To learn more about UBB code tags, you can reference this page. The [code] tag is discussed about 3/4 down the page.
You can fix the above post by clicking the edit button (the one with the pencil and paper), highlighting the code, then clicking the "code' button below the editing window. Then click the "Edit Post" button to submit the change.
To answer your question, since you are using it outside the loop, you have to initialize the min variable outside the loop. There is a reason why you need to do this. Looking at the code you have right now, if someone were to run it and enter 0 for n what would happen? Would anything inside the for loop run? And what happens when the line:
runs? If the code in the loop doesn't run, what is min's value when it gets used in that line of code?
Think about those questions a bit and I think it will help you understand why you need to initialize the min variable. See if that helps. Then post what you think you need to do to fix the issue and we can let you know if you are on the right track. If you still aren't seeing the solution, post what you think are the answers to these questions and we can help you get to the next step in solving the problem. [ May 31, 2008: Message edited by: Mark Vedder ]
If I initialize it outside the loop it would print out the first number only.
If min is initialized outside the loop, what happens to its value in line 30 below? What value then gets used in the rest of the for loop? The initialized value?
[ May 31, 2008: Message edited by: Mark Vedder ]
Joined: May 31, 2008
Hi Mark, Thanks for the advice. You are right I had to initialize the min value to 0 first befor the loop. But the code still prints the wrong value. I always get the value next to the last. I guess the code inside the loop is wrong and probably the swap section...
Just to make sure I am understanding the goal of the code, it is to print out the smallest value that's in the array. Is that correct?
And if so, do you just need to know (and print out) the minimal value, or do you need to know the index of that minimal value in the array? In other words do you need to know that the minimal values was in, say, index 3 of the array?
Looking at the for loop, you may be over-thinking the problem a little bit and making it a little more complex than it needs to be. That's not uncommon when first learning to program. If you can clarify the requirements we can guide you in reworking the logic in that for loop so it does what it should do.
Joined: May 31, 2008
Yes, you are correct. I'm trying to find and print the smallest number in an array. I know the code could be simpler. I can just compare two consecutive values in a single loop and assign the smaller one to the 'min' value but it still doesn't show the right number... I've tried this way:
Well, the good news is that you are much closer. I think where you are getting caught up a little is that you seem to be worrying about other numbers in the array rather than just the current one (when looping).
If we break down into words what we want to do, we want to do the following:
Loop through the array.
For every value in the array, determine if the current value is smaller than the last remembered smallest value.
If the current value is smaller that the last remembered (i.e. stored) value, remember it (and forget the previous value). If it is not, do nothing with the current value and go to the next value.
You'll notice in that description, we don't talk at all about "the next value in the array". And we don't talk about comparing the current number to any other numbers in the array. We only need to compare it to the previously smallest number. So take a look at your code and see if you can determine what you need to change. If you get stuck, post what you have and where you are getting stuck.
Joined: May 31, 2008
Thanks Mark, I think I've got it. I made a small change and I think it works right. I tried to find the biggest number with one more if condition (with opposite comparison) and it worked too.
Now there is one more small thing you can do to tweak it a little bit. What you have will work perfectly. But there is one minor thing that we can change to make it perfect.
Think about what happens the first time through the 'for' loop. At that point the following is true:
min = the value at index 0 of the array.
i will equal zero.
Because of that, the line:
is basically saying
the first time through the for loop. Now we know that will never be true. So while it doesn't hurt anything being there, it is needlessly checking something that will always be false. By changing one simple character -- note I said change and not add -- in the block of code you pasted above in your last post, we can avoid that useless check and thus optimize the code a little bit. Can you think of what the change is?
As a side note, let me say this about optimization... you generally do not want to optimize at the cost of clarity - i.e. at the cost that it is clear what the code is doing. And you want to comment why you are doing what you are doing. And generally you only want to optimize after you have a good solid working version of the code. (Then be sure to save a copy of that working version so you can fall back to it.)
I only bring up the idea of optimizing the for loop since in this case I think it is more about having a clearer understanding of how to write a "find the minimum value in an array" algorithm than it is solely about optimizing. By making the change, I think it will help you understand the algorithm a little better and how it truly functions. [ June 01, 2008: Message edited by: Mark Vedder ]