smallest element

Find the smallest element in an array of integers

Page 1 of 1

2 Replies - 2456 Views - Last Post: 22 October 2007 - 02:36 AM Rate Topic: -----

#1 m_milam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-September 07

smallest element

Posted 21 October 2007 - 06:40 PM

I am supposed to find the smallest element in an array of integers. I was asked to use {1, 2, 4, 5, 10, 100, 2, -22} as the integers to test this against. Here is my code. I do not get any errors but I do not get any results either. I have a feeling I am close.

/**
 * Class:  BMIS 3301 Web Programming with Java
 * Instructor: Jingshan Xiao
 * Description: Reversing Numbers : Exercise 6_3
 * Due: 10/14/2007
 * @author Matthew Milam
 */
 import javax.swing.JOptionPane;
 
 public class Exercise6_9f{ 
  // Main method
  public static void main(String[] args) {
	int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};

  }

  public static int smllElement(int[] numbers) {
	int max = numbers[0];
	for (int i = 0; i < numbers.length; i++) {
		if ( max > numbers[i] )
			max = numbers[i];
	}
	
	//Prepare Result
	String output = "The array is ";
	for (int i = 0; i < numbers.length; i++) {
		output += numbers[i] +"";
	}
	
	output +="\nThe Smallest Element is " + max;
	
	//Display Result
	JOptionPane.showMessageDialog(null, output);
  return max;
 }
 }



Anyone? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: smallest element

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: smallest element

Posted 21 October 2007 - 09:20 PM

You are very close, the only thing you are forgetting is to call the method from inside your main method. I'm not sure why you need your method to return a value, but currently it does.

  public static void main(String[] args) {
	int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};
	// need to call the method from main
	int maxNum = smllElement(numbers);

  }


Good job!
Was This Post Helpful? 0
  • +
  • -

#3 m_milam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-September 07

Re: smallest element

Posted 22 October 2007 - 02:36 AM

View Postjayman9, on 21 Oct, 2007 - 09:20 PM, said:

You are very close, the only thing you are forgetting is to call the method from inside your main method. I'm not sure why you need your method to return a value, but currently it does.

  public static void main(String[] args) {
	int[] numbers = {1, 2, 4, 5, 10, 100, 2, -22};
	// need to call the method from main
	int maxNum = smllElement(numbers);

  }


Good job!



Thanks for the insight. Whenever I am stuck I am usually a line or two off.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1