5 Replies - 10138 Views - Last Post: 10 September 2009 - 07:42 PM Rate Topic: -----

#1 deb4te  Icon User is offline

  • D.I.C Head

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

smallest element in an array?

Post icon  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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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);
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: smallest element in an array?

Posted 10 September 2009 - 07:26 PM

View Postdeb4te, 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
Was This Post Helpful? 0
  • +
  • -

#5 deb4te  Icon User is offline

  • D.I.C Head

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

Re: smallest element in an array?

Posted 10 September 2009 - 07:29 PM

View Postpbl, on 10 Sep, 2009 - 06:26 PM, said:

View Postdeb4te, 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.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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 :D

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;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1