Monday, March 23, 2009

Make this simpler?

/* * To change this template, choose Tools Templates * and open the template in the editor. */
package recursion;
/** * * @author dfreer */
public class Main {
public static void countDown(int x) {

//make a recursive method that counts down from itself until 0 and
//also prints out each number...go down by one!!
if(x == 0)
{

}
else
{
System.out.println(x);
countDown(x-1);
}
}

public static int calcMinimum(int [] a, int counter, int minimum)
{
if(counter == a.length-1)
{
if(a[a.length-1] < minimum)
{
return a[a.length-1];
}
else
return minimum;
}
else
{
if(a[counter] < minimum)
{
minimum = a[counter];
System.out.println("We got here!");
return calcMinimum(a, ++counter, minimum);
}
return calcMinimum(a, ++counter, minimum);
}
}

public static void main(String[] args) { // TODO code application logic here
int [] b = {7,16,23,-55}; //expecting -55
System.out.println(calcMinimum(b,0,b[0]));
//countDown(10);
//start counter at 0 and minimum at 0 }}

No comments: