Computer Science Canada Little qick question. |
Author: | Caceres [ Sun Jul 23, 2006 9:57 pm ] | ||
Post subject: | Little qick question. | ||
Hey, I've bene working on this program for HOURS! I'm a noob so it take sme forever. ![]() Anyways.. I just ran into a very minor error. I do a statement to find the minimum value, but the termaining code is "-1". So it always returns the -1 as the minimum value. My whole program is as follows:
I was just wondeing how to fix the "minimum" problem. Thanks. Baically: a statement that would not include the "-1" into the minimum. Thanks. ~Caceres |
Author: | OneOffDriveByPoster [ Sun Jul 23, 2006 10:21 pm ] | ||
Post subject: | |||
You could try using a break statement:
basically tells the computer to "break out of the loop" if you get -1. That should also simplify your loop-condition. Be careful to review how adding the break statement changes your program. |
Author: | Caceres [ Sun Jul 23, 2006 10:22 pm ] |
Post subject: | |
Where would I add that statemtn in? Right before the minimum? |
Author: | OneOffDriveByPoster [ Sun Jul 23, 2006 10:27 pm ] |
Post subject: | |
Caceres wrote: Where would I add that statemtn in? Right before the minimum?
Part of programming is knowing how to use the constructs you learn to get the desired behaviour. I believe that pointing you to "break" is about as much as I would do. |
Author: | wtd [ Sun Jul 23, 2006 10:40 pm ] | ||
Post subject: | |||
Amazingly enough, well-formatted code is easier to reason about.
You have a major flaw in your logic, though. You keep referring to elements in an array before you've initialized them. |
Author: | Caceres [ Sun Jul 23, 2006 10:42 pm ] |
Post subject: | |
Thanks wtd. I'm just curious if there's such thing as: if(number[count] < minimum) { minimum!=-1; minimum=number[count]; } The minimum!=-1 doesn't work, but I was wondeirng if there's a statement I can add there, that'll erase the -1 data. THanks. And by the way, does wtd..mean What the duck? ~Caceres |
Author: | wtd [ Sun Jul 23, 2006 11:23 pm ] |
Post subject: | |
Caceres wrote: Thanks wtd.
I'm just curious if there's such thing as: if(number[count] < minimum) { minimum!=-1; minimum=number[count]; } The minimum!=-1 doesn't work, but I was wondeirng if there's a statement I can add there, that'll erase the -1 data. THanks. No. You can only set it to another value. You cannot set it to some undefined state. Quote: And by the way, does wtd..mean What the duck?
~Caceres No, and yes. As for your Java problem... perhaps you should concentrate on reading in the grades, and then worry about finding the average, min and max. You will need at least one value known, after all, if you wish to make comparisons to figure out the minimum and maximum. |