0

This is regularly how it's done but it has to be recursively with no for, do-while, and while loops. if statements only.

import java.util.ArrayList;
import java.util.Scanner;

public class arrayex1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        System.out.println("Enter numbers: ");

        for (int i = 0; i < 10; i++) {
            int num = input.nextInt();
            numbers.add(num);
        }

        for (int i = 0; i < numbers.size(); i++) {
            if (numbers.get(findMin(numbers)) == i) { // If the 'smallest' index value is equal to i.
                System.out.println(numbers.get(i) + " <== Smallest number");
            } else {
                System.out.println(numbers.get(i));
            }
        }
    }

    public static int findMin(ArrayList<Integer> n) {

        int min = 0; // Get value at index position 0 as the current smallest.

        for (int i = 0; i < n.size(); i++) {
            if (n.get(i) < min) {
                min = i;
            }
        } 

        return min;
    }
}
2
  • What is the problem with the current code? Are you asking how to remove the existing for loops? If so, you should try it yourself, and ask a question when you get stuck with a specific bug. Commented Mar 18, 2013 at 3:13
  • no for loop, do-while loops, and no while loops only if statements these were the parameters set by my professor and i am really bad with recursion to the point where i don't understand any of it really. The code I originally posted is with all the loops but i didn't know how to even approach it recursively. Commented Mar 18, 2013 at 3:34

3 Answers 3

2

One way you could code it:

findMin should return int, take ArrayList<Integer> integers, int min, int index and be called with (integers, Integer.MAX_VALUE, 0).

findMin should check to see if the value of integers[index] is smaller than min - if it is, it updates min.

Then, if it is not at the last index in integers, it'll return the value of calling itself with (integers, min, ++index).

If it is, it will return min.

Sign up to request clarification or add additional context in comments.

4 Comments

is this recursion? the parameters of the question is it has to be done recursively...
@Suzy M Woodruff Yep! Think about it - The flow control of the method is calling itself.
I may be over thinking this but we can't use for loops, do-while loops, and no while loops...if statements only so how will this work? could someone dummy it down for me?
@Suzy M WoodRuff I only used if and calling. Isn't that exactly what you wanted?
1

Here you go ...

public static void main(String[] args) throws Exception {

    final List<Integer> numbers = new ArrayList<Integer>() {
        {
            add(3);
            add(4);
            add(6);
            add(1);
            add(9);
        }

    };

    final int min = findSmallest(numbers.iterator(), Integer.MAX_VALUE);
    System.out.println("Smallest: " + min);
}

private static int findSmallest(Iterator<Integer> iterator, Integer max) {

    int min = Math.min(iterator.next(), max);
    if (iterator.hasNext()) {
        min = findSmallest(iterator, min);
    }

    return min;
}

1 Comment

Op said it needed to take an ArrayList<Integer>, but not sure how hard of a requirement that is...
0

You could do something like this.

int min = 2876529394; // Holds the smallest element. Put a number that you know won't 
                      // be in the ArrayList just to make the code simpler. If you don't
                      // have such a number, just implement a counter variable.

findMin( numbers, 0 );
public void findMin( ArrayList<Integer> a, int index ) {

    if( index < a.size() ) {

        if( a.get( index ) < min )
             min = a.get( index );

       findMin( a, ++index );

   }
}            

Here, you're essentially doing the exact same thing a for-loop would do in principle, but instead you're using recursion.

Comments

Your Answer

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.