1

Ths is a question from a past paper. I have been asked to create a static method arrayMin to find the smallest value in the array arr.

I have to use a while loop and on each iteration, the variable min will return the smallest number from the first i elements.

Is there a way to do this without calling another method/for loop and strictly using the while loop, as the question is only worth 4%(including writing loop invariants and javadoc). Not sure if I am overcomplicating the problem.

public class Revision {
public static int arr[] = new int[] { 5, 8, 4, 3, 6, 2 };
public static int min = 1;
public static int arrayMin() {
 int i = 0;
 if (arr == null) {
 return 0;
 } else {
 while (i < arr.length) {
 // some function/method call to find smallest number of arr[i]
 i++;
 return min;
 }
 }
 return min;
}
public static void main(String[] args) {
 System.out.println(arrayMin());
}

}

asked Aug 25, 2012 at 13:07
3
  • 1
    In your code you will always return 1 if arr != null and otherwise 0 because of your return min statement in the while loop. Commented Aug 25, 2012 at 13:11
  • I think it is a nice optimization to return the minimum value in the range that the values in the range can take for each element in the array. Note that in Java, 0 may not be the minimum, integers may have negative values. Commented Aug 25, 2012 at 13:15
  • A value of null normally does not mean the same as an array of 0 elements. You are better off checking against null and throwing a NullPointerException or IllegalArgumentException if you encounter null for the array. Commented Aug 25, 2012 at 13:20

4 Answers 4

8

A couple of things:

  1. The array shouldn't be static, you should pass it as a parameter to the arrayMin method;
  2. min should be a local arrayMin variable, not static;
  3. min should be initialized to Integer.MAX_VALUE. If you initialize it with 1, and 2 happens to be the min value of the array, you'll never return it;
  4. You can't return multiple times from a method. As soon as you do return min, the method ends. There's probably some confusion over the the variable min will return the smallest number from the first i elements phrase. It probably means that in each iteration, the variable min will have (not return) the smallest number from the first i elements.

Here's a refactor:

public static int arrayMin(int[] arr) {
 int i = 0;
 int min = Integer.MAX_VALUE;
 if (arr == null) {
 return 0; // What if 0 is the minimum value? What do you want to do in this case?
 } else {
 while (i < arr.length) {
 if (arr[i] < min) {
 min = arr[i];
 }
 i++;
 }
 }
 return min;
}
answered Aug 25, 2012 at 13:15

1 Comment

+1 for the additional explanations, but please don't return full code for programming assignments.
6

You need to have a variable outside of the loop called min. You will use the loop to find the minimum of the array, and return min when the loop is complete.

} else {
 int min = Integer.MAX_VALUE;
 while(i < arr.length) {
 // is arr[i] < min? If so, it's the new minimum
 i++;
 }
 return min;
}
answered Aug 25, 2012 at 13:12

Comments

0

multiple ways to do it, but here is one. public static int arrayMin(int[] arr) {

 boolean isFirstElement = true;
 int smallestNumber= 0;
 int index = 0;
 while(index < arr.length) {
 int temp= arr[index];
 index++;
 if (isFirstElement) {
 smallestNumber = temp;
 isFirstElement = false;
 } else if (smallestNumber > temp) {
 smallestNumber = temp;
 }
 }

}

answered Jun 28, 2013 at 14:39

Comments

0

You can use a index variable to keep in track of the number of positive hits and if the corresponding numbers index value is one lesser the array size, that number is the smallest

class testtt{
 static int small=0;
 public static void main(String[] args) {
 int arr[] = {9,2,3,4,5,6,7,8}; 
 int i,index=0; 
 for(int q:arr)
 { 
 for(i=0;i<arr.length;i++)
 { 
 if(q<arr[i])
 {
 small=q;
 index++;
 }
 }
 if(index==arr.length-1)
 System.out.println(small); 
 } 
 }
}
answered Aug 14, 2015 at 5:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.