Finding Min&Max Values in Arrays...

Page 1 of 1

8 Replies - 844 Views - Last Post: 27 September 2011 - 02:17 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248817&amp;s=5f664c481e2e986c31bb7713f23f8a13&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mnosek3

Reputation: 0
• Posts: 32
• Joined: 10-September 11

Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:01 PM

In my class I am trying to find the smallest pet and largest pet based on weight. The method to find the max value seems to work fine but the minimum method is returning the wrong value and I can't figure out why. Could some one please help me with this?

```package ids401.labtwo;

public class Pet {

private String name;

private int age; // in years

private double weight; // in pounds

public Pet() // default constructor

{

name = "XXXXXX";

age = 0;

weight = 0;
}

public Pet(String name, int age, double weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}

public static String getLargestPet(Pet[]pet)

{
String largestName = pet[0].name;

double maxValue = pet[0].weight;

for(int i = 0; i<pet.length;i++){

if(pet[i].weight>maxValue){

largestName = pet[i].name;
}
}
return largestName;

}

public static String getSmallestPet(Pet[]pet)
{
String smallestName = pet[0].name;

double minValue = pet[0].weight;

for(int i = 0; i<pet.length;i++){

if(pet[i].weight<minValue){

smallestName = pet[i].name;
}
}

return smallestName;

}

```

```package ids401.labtwo;

public class PetTest {

public static void main(String[] args) {

int MAX_NUMBER =5;

Pet pet [] = new Pet[MAX_NUMBER];

pet[0]= new Pet("Timber",5,120);
pet[1]= new Pet("Piper",2,20);
pet[2]= new Pet("Reba",4,50);
pet[3]= new Pet("Fluff",1,30);
pet[4]= new Pet("Wilson",9,75);

System.out.println("Largest Pet = "+ Pet.getLargestPet(pet));
System.out.println("Smallest Pet = "+ Pet.getSmallestPet(pet));

```

Is This A Good Question/Topic? 0

Replies To: Finding Min&Max Values in Arrays...

#2 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:12 PM

Could you tell us what output you're actually getting?

#3 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:13 PM

How is it not returning the right one? What is it returning for smallestPet method?

As smohd pointed out you never assign the min and max value the new value in the if statement where you assigned the new name. The only reason why it worked for the Max is because the entry at index 0 actually is the heaviest.

This post has been edited by Fuzzyness: 27 September 2011 - 01:21 PM

#4 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:16 PM

You missed onething in your if condition, you also have to update the minValue so it get the new smallest value. This should be in if:
```if(pet[i].weight<minValue){
smallestName = pet[i].name;
minValue = pet[i].weight;
```
also for finding the maximum is the same case.
NOTE: Your loop should start at i = 1

#5 mnosek3

Reputation: 0
• Posts: 32
• Joined: 10-September 11

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:51 PM

Pontus, on 27 September 2011 - 01:12 PM, said:

Could you tell us what output you're actually getting?

The output is

Largest Pet = Timber
Smallest Pet = Wilson

smohd, on 27 September 2011 - 01:16 PM, said:

You missed onething in your if condition, you also have to update the minValue so it get the new smallest value. This should be in if:
```if(pet[i].weight<minValue){
smallestName = pet[i].name;
minValue = pet[i].weight;
```
also for finding the maximum is the same case.
NOTE: Your loop should start at i = 1

Thanks for clearing this up. Why dose the minValue statement have to be included in the if statemet?
Why dose the loop have to start at 1?

This post has been edited by mnosek3: 27 September 2011 - 01:55 PM

#6 mnosek3

Reputation: 0
• Posts: 32
• Joined: 10-September 11

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 01:57 PM

Fuzzyness, on 27 September 2011 - 01:13 PM, said:

How is it not returning the right one? What is it returning for smallestPet method?

As smohd pointed out you never assign the min and max value the new value in the if statement where you assigned the new name. The only reason why it worked for the Max is because the entry at index 0 actually is the heaviest.

I changed the value of index 3 to 150 and my method for max worked fine...

#7 mnosek3

Reputation: 0
• Posts: 32
• Joined: 10-September 11

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 02:10 PM

Here are my three methods the first to for smallest and largest work fine. But I am having trouble with understading the oldestPet method. Could someone explain what I am doing wrong? Thanks for all your help! This site has been a huge help in my process of learning java code.

```public static String getLargestPet(Pet[]pet)

{
String largestName = pet[0].name;

double maxValue = pet[0].weight;

for(int i = 1; i<pet.length;i++){

if(pet[i].weight>maxValue){

largestName = pet[i].name;
maxValue = pet[i].weight;
}
}
return largestName;

}

public static String getSmallestPet(Pet[]pet)
{
String smallestName = pet[0].name;

double minValue = pet[0].weight;

for(int i = 1; i<pet.length;i++){

if(pet[i].weight<minValue){

minValue = pet[i].weight;
smallestName = pet[i].name;
}
}

return smallestName;

}

public static String getOldestPet(Pet[]pet)
{

String oldestName = pet[0].name;

int maxAge = pet[0].age;

for(int i = 0;i<pet.length;i++){

if(pet[i].age>maxAge){

oldestName = pet[i].name;
maxAge = pet[i].age;
}
}

return oldestName;

}

```

Output:

Largest Pet = Timber
Smallest Pet = Piper
Oldest Pet = Timber ***Wrong

This post has been edited by mnosek3: 27 September 2011 - 02:18 PM

#8 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 02:10 PM

mnosek3, on 28 September 2011 - 02:36 AM, said:

Thanks for clearing this up. Why dose the minValue statement have to be included in the if statemet?
Why dose the loop have to start at 1?

Because the value of the name assigned is now the minimum value, so other comings must be checked with that one and not initial assigned.
For example if I have a list 10,2,7,4,6... and I need to find the min, starting to set 10 as min, then check if 2 is less than 10 and get yes, so now I have to set min = 2 because it is now the minimum otherwise if I leave 10 the all other numbers will accept to be less than 10.

You have to started with 1 because 0 has already been used before loop, so the first iteration will be comparing the same item...
```double maxValue = pet[0].weight;
//at the first iteration will be
if(pet[0].weight<minValue){//so checking the same value which you have assigned it to minValue
```

#9 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

Re: Finding Min&Max Values in Arrays...

Posted 27 September 2011 - 02:17 PM

First, this lines of code from getLargestPet() method are wrong:
```if(pet[i].weight>maxValue){

largestName = pet[i].name;
maxValue = pet[0].weight; // this should be maxValue = pet[i].weight
}
```

Also in getOldestPet() method, you have wrong statement:
```oldestName = pet[0].name;
maxAge = pet[0].age;

//SHOULD BE:
oldestName = pet[i].name;
maxAge = pet[i].age;
```