1 Replies - 131 Views - Last Post: 07 April 2010 - 08:45 PM Rate Topic: -----

#1 justin23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

finding higher and lower values in arrays

Posted 07 April 2010 - 08:32 PM

	public double high(){
		for (int i = 0; i < 30; i++){
			if (temperature[i] > temperature[i+1]){
				high = temperature[i];
			}
			else {
				high = temperature[i+1];
			}
			return high;
		}
	}
	
	public double low(){
		for (int i = 0; i < 30; i++){
			if (temperature[i] < temperature[i+1]){
				low = temperature[i];
			}
			else {
				low = temperature[i+1];
			}
		}
		return low;
	}
	

these are methods for returning the lowest and highest temperatures of the month, specifically months that have 31 days.
would i be able to turn this into my teacher and receive full credit or should i use a different method?

Is This A Good Question/Topic? 0
  • +

Replies To: finding higher and lower values in arrays

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: finding higher and lower values in arrays

Posted 07 April 2010 - 08:45 PM

What if the temperature array looked like this (pretend the for loop was modified to allow for this 4 day month): {-1, 2, 1, 4} (it was a particularly cold 4 day month)

On the first iteration of low's loop, low would be set to -1, since it is less than 2
On the second iteration of low's loop, low would be set to 1, since it is less than 2 //even though -1 is lower
On the third iteration of low's loop, low would be set to 1, since it is less than 4

The lowest number in the array is obviously -1, but your method reports it to be 1.

Instead of comparing temperature[i] to the next element in the array, compare it to the current value of low. You would also remove the else in this circumstance.

The same principle applies for the high method

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1