limadog2003
limadog2003

Reputation: 33

Trying to determine smallest amount and position in a Java array

I am new to the site, and new to programming in general, so please be gentle. I'm doing an assignment and I've built everything it needs, but I'm having problems with one particular part.

So the code below is just the for loop part of the overall program that I'm having trouble with. I created an array to store the user's quarterly input. When I go to determine which quarter had the largest rainfall, the code works no problem.

Where I'm having problems however is when I try to determine the quarter with the least amount. I basically set them both up the same way, but it doesn't work. I'm sure I must have a logic problem somewhere in there, but I've racked my brain trying to figure it out. Any help would be greatly appreciated. I'm sure its something small and stupid that I'm doing.

double largest=0;
for (int k = 0; k < quarterlyArray.length; k++) { // Determines the largest quarterly rainfall.
  if ( quarterlyArray[k] >= largest ) {
    largest = quarterlyArray[k];
  }
}

for (int k = 0; k < quarterlyArray.length; k++) { // Determines in which quarter had largest rainfall.
  if (quarterlyArray[k] == largest) {
    System.out.println("Quarter " + (k+1) + " saw the most rain this year with " + largest + " inches.");
  }
}

double smallest = 0;  
for (int p = (quarterlyArray.length-1); p == 0; p--) { // This is not working right now. 
  if (quarterlyArray[p] < quarterlyArray [quarterlyArray.length-1] ||  quarterlyArray[p] < smallest) {
    quarterlyArray[p] = smallest;
  }
}

for (int l = quarterlyArray.length; l == 0; l--) { // Nor this.
  if (quarterlyArray[l] == smallest) {
    System.out.println("Quarter " + (l+1) + " saw the least rain this year with " + smallest + " inches.");
  }
}

Upvotes: 3

Views: 94

Answers (3)

FredK
FredK

Reputation: 4084

Your loop for the smallest is all wrong - you are setting the array values, not finding the smallest. Do it all in one loop:

double largest=Double.MIN_VALUE;
double smallest = Double.MAX_VALUE;
for (int k = 0; k < quarterlyArray.length; k++) {
  if ( quarterlyArray[k] > largest ) {
     largest = quarterlyArray[k];
  }
  if ( quarterlyArray[k] < smallest ) {
     smallest = quarterlyArray[k];
  }
}

You can also refine this in the manner shown by @mkobit to make it a bit faster:

double largest=quarterlyArray[0];
double smallest = largets;
for (int k = 1; k < quarterlyArray.length; k++) {
  if ( quarterlyArray[k] > largest ) {
     largest = quarterlyArray[k];
  } else if ( quarterlyArray[k] < smallest ) {
     smallest = quarterlyArray[k];
  }
}

Of course, you should first check to ensure that quarterlyArray has a length greater than zero.

Upvotes: 1

mkobit
mkobit

Reputation: 47249

A few things that I notice, that are contributing to your problem:

for (int p = (quarterlyArray.length-1); p == 0; p--) {

The p == 0 is the termination expression. Your loop will only iterate while this condition is true. Your loop will not execute because the condition is false on the first iteration.

for (int l = quarterlyArray.length; l == 0; l--) {

Same problem in this loop, too. Look at your condition in your first for loop - k < quarterlyArray.length. When this is false, your loop will terminate.

quarterlyArray[p] = smallest

You are modifying the array rather than your local variable smallest.

You also need to be careful about bounds of the arrays. Your loop sets l to the array length - int l = quarterlyArray.length. If you then try to index into this array using quarterlyArray[l] you would get an ArrayIndexOutOfBoundsException because Java arrays (and most programming languages I would say) are 0-based.

Another thing to point out is how you are calculating both the min and max. Say, for example, that your quarterlyArray contains all negative values. What would largest be? Well, none of the items in the array are greater than 0, so largest would be 0. That would be incorrect because that value is not in the array.

In your first case, it looks like you are starting with an element that you know will always be lower than the elements in the array. This works out fine if you know all the elements will be greater than that. Your double largest = 0 will be changed to the maximum value of the array based on the constraints of the problem you are solving.

A better way to approach this is to start with a known element from the array. If you simply change your largest to be the first/last element from the array, and then go through the rest, you will always come out with the desired result.

double largest = quarterlyArray[0];
for (int k = 1; k < quarterlyArray.length; k++) { // Determines the largest quarterly rainfall.
  if ( quarterlyArray[k] >= largest ) {
    largest = quarterlyArray[k];
  }
}

Notice how all I changed was the initialization of largest, and int k = 1. You can do the same thing for smallest. In this case, I start from the end of the array.

double smallest = quarterlyArray[quarterlyArray.length - 1];  
for (int p = quarterlyArray.length - 2; p >= 0; p--) {right now. 
  if (quarterlyArray[p] < smallest) {
    smallest = quarterlyArray[p];
  }
}

Another note for after you get through this, is how could do both in 1 for loop?

Upvotes: 1

singhakash
singhakash

Reputation: 7919

Start the last loop form quarterlyArray.length-1.Change to

for (int l = quarterlyArray.length-1; l >= 0; l--)

The last element of the array would be at quarterlyArray.length-1 not at quarterlyArray.length

And l == 0(termination expression) should be l >= 0 because when the termination expression evaluates to false, the loop terminates.

Upvotes: 1

Related Questions