/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code9nov17; /** * * @author jcmtiernan */ public class Code9Nov17 { /** * @param args the command line arguments */ public static void main(String[] args) { // methods, arrays, powers of 2, recursion final int MAXPOWERS2 = 50; long[] powers2; powers2 = new long[MAXPOWERS2]; for (int n = 0; n < MAXPOWERS2; n=n+1) { System.out.println("powers2["+n+"] = "+powers2[n]); } /* for (int n = 0; n < MAXPOWERS2; n++) { powers2[n] = (long) Math.pow(2.0,n); // powers2[n] = 2 * powers2[n-1]; } */ System.out.println("\n\nFillPowers"); fillPowers(powers2); System.out.println("\n\nFillPowersRep"); //fillPowersRep(powers2); System.out.println("\n\nFillPowersRec"); //fillPowersRec(powers2,(MAXPOWERS2 - 1)); System.out.println("\n"); int count = 0; for (long value: powers2) { System.out.println("powers2["+ count++ +"] = "+ value); //System.out.println("Array value = "+value); } } public static void fillPowers(long[] array) { for (int n = 0; n < array.length ; n++) { array[n] = (long) Math.pow(2.0,n); System.out.println("in fillPowers, n = "+n); } //return; } public static void fillPowersRep(long[] array) { array[0] = (int) Math.pow(2,0); for (int n = 1; n < array.length ; n++) { array[n] = 2 * array[n-1]; System.out.println("in fillPowersRep, n = "+n); } return; } public static long fillPowersRec(long[] array, int n) { if (n == 0) { return (array[0] = (int) Math.pow(2,0)); } System.out.println("in fillPowersRec, n = "+n); return (array[n] = 2 * fillPowersRec(array,n-1)); } }