smallest element in an array?

Page 1 of 1

5 Replies - 12812 Views - Last Post: 10 September 2009 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=125113&amp;s=a068d96b353ee26d733defa268682789&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 deb4te

Reputation: 0
• Posts: 50
• Joined: 22-January 09

smallest element in an array?

Posted 10 September 2009 - 07:12 PM

I have this code that finds the largest number in an array here:
```int maxIndex = 0;
for (index = 1; index < list.length; index++)
if (list[maxIndex] < list[index])
maxIndex = index;
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);
```

I need to somehow modify this so that it finds the smallest number in an array... i can't seem to figure out what i need to change... any tips/help? Thanks in advance!

Is This A Good Question/Topic? 0

Replies To: smallest element in an array?

#2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: smallest element in an array?

Posted 10 September 2009 - 07:15 PM

you can set the smallest number to be the first element in the array, start the iteration from index 1. on each iteration check if the current is smaller than the preset smallest, if it is the set the smallest to be the current element

#3 deb4te

Reputation: 0
• Posts: 50
• Joined: 22-January 09

Re: smallest element in an array?

Posted 10 September 2009 - 07:18 PM

ahhh i'm sorry, i should have mentioned that the program reads in 10 numbers. here, i'll just post my whole prog...

```import java.util.*;
public class PL2H2_1
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int[] list = new int[10];
int sum, index, biggestnumber, smallestnumber;

System.out.println("Enter ten integers: ");
list[0] = console.nextInt();
list[1] = console.nextInt();
list[2] = console.nextInt();
list[3] = console.nextInt();
list[4] = console.nextInt();
list[5] = console.nextInt();
list[6] = console.nextInt();
list[7] = console.nextInt();
list[8] = console.nextInt();
list[9] = console.nextInt();

sum = list[0] + list[1] + list[2] + list[3] + list[4] + list[5] + list[6] + list[7] + list[8] + list[9];

System.out.println("The sum of the numbers = " + sum);
System.out.print("The numbers in reverse order are: ");
System.out.println(list[9] + " " + list[8] + " " + list[7]
+ " " + list[6] + " " + list[5] + " " + list[4]
+ " " + list[3] + " " + list[2] + " " + list[1]
+ " " + list[0]);
int maxIndex = 0;
for (index = 1; index < list.length; index++)
if (list[maxIndex] < list[index])
maxIndex = index;
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);

int minIndex = 0;
for (index = 0; index < list.length; index++)
if (list[minIndex] < list[index])
minIndex = index;
smallestnumber = list[minIndex];
System.out.println("The smallest number you entered was: " + smallestnumber);
}
}

```

#4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Re: smallest element in an array?

Posted 10 September 2009 - 07:26 PM

deb4te, on 10 Sep, 2009 - 06:12 PM, said:

I have this code that finds the largest number in an array here:
```int maxIndex = 0;
for (index = 1; index < list.length; index++)
if (list[maxIndex] < list[index])
maxIndex = index;
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);
```

I need to somehow modify this so that it finds the smallest number in an array... i can't seem to figure out what i need to change... any tips/help? Thanks in advance!

Indent you code properly and use {} it is very confusing to have a if that follows a for and have other statements after
```int maxIndex = 0;
for (index = 1; index < list.length; index++) {
if (list[maxIndex] < list[index])
maxIndex = index;
}
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);

```

Should work

#5 deb4te

Reputation: 0
• Posts: 50
• Joined: 22-January 09

Re: smallest element in an array?

Posted 10 September 2009 - 07:29 PM

pbl, on 10 Sep, 2009 - 06:26 PM, said:

deb4te, on 10 Sep, 2009 - 06:12 PM, said:

I have this code that finds the largest number in an array here:
```int maxIndex = 0;
for (index = 1; index < list.length; index++)
if (list[maxIndex] < list[index])
maxIndex = index;
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);
```

I need to somehow modify this so that it finds the smallest number in an array... i can't seem to figure out what i need to change... any tips/help? Thanks in advance!

Indent you code properly and use {} it is very confusing to have a if that follows a for and have other statements after
```int maxIndex = 0;
for (index = 1; index < list.length; index++) {
if (list[maxIndex] < list[index])
maxIndex = index;
}
biggestnumber = list[maxIndex];
System.out.println("The largest number you entered was: " + biggestnumber);

```

Should work

sorry bout the indenting. yes, that works for largest element, but i need to find the smallest.

#6 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Re: smallest element in an array?

Posted 10 September 2009 - 07:42 PM

just replace

if (list[maxIndex] < list[index])
maxIndex = index;

by

if (list[maxIndex] > list[index])
maxIndex = index;

or do 2 both at the same time

```int maxIndex = 0, minIndex = 0;
for (index = 1; index < list.length; index++) {
if (list[maxIndex] < list[index])
maxIndex = index;
if(list[minIndex] > list[index])
minIndex = index;
}

```