001package algs11;
002import stdlib.*;
003import java.util.Arrays;
004/*
005 PROBLEM 1: Run the program algs11.XAutoboxingValueOf and answer the following questions.
006
007   QUESTION: What is the difference between the following two statements:
008     x = "Dog";
009     x = new String("Dog");
010
011   ANSWER:
012
013   QUESTION: What is the difference between the following two statements:
014     x = Integer.valueOf (3);
015     x = new Integer (3);
016
017   ANSWER:
018
019   QUESTION: Aside from the difference in the numbers,
020   what is the difference between the following two statements:
021     x = Integer.valueOf (12);
022     x = Integer.valueOf (132973);
023
024   ANSWER:
025
026 PROBLEM 2: Using the formula given in class, write recursive versions of "average", "copy" and
027"reverse", using the iterative versions below as a starting point.  The iterative versions are
028"averageI", "plusOneI" and "reverseI".  HINT: You will need to define helper functions.
029 */
030public class MyArrayFunctions {
031
032        public static double max (double[] a) {
033                return 0.0; // TODO
034        }
035        public static double maxI (double[] a) {
036                int N = a.length;
037                double result = Double.NEGATIVE_INFINITY;
038                for (int i = 0; i < N; i++)
039                        if (a[i] > result) result = a[i];
040                return result;
041        }
042
043        public static double[] plusOne (double[] a) {
044                return null; //TODO
045        }
046        public static double[] plusOneI (double[] a) {
047                int N = a.length;
048                double[] result = new double[N];
049                for (int i = 0; i < N; i++)
050                        result[i] = a[i] + 1;
051                return result;
052        }
053
054        public static void reverse (double[] a) {
055                return; //TODO
056        }
057        public static void reverseI (double[] a) {
058                int N = a.length;
059                for (int i = 0; i < N / 2; i++) {
060                        double temp = a[i];
061                        a[i] = a[N - 1 - i];
062                        a[N - i - 1] = temp;
063                }
064        }
065
066        public static void test (double[] a) {
067                StdOut.format ("max: %f\n", maxI (a));
068                double[] b = plusOne (a);
069                StdOut.format ("plusOne: %s\n", Arrays.toString (b));
070                reverse (a);
071                StdOut.format ("reverse: %s\n", Arrays.toString (a));
072        }
073        public static void testI (double[] a) {
074                StdOut.format ("max: %f\n", maxI (a));
075                double[] b = plusOneI (a);
076                StdOut.format ("plusOne: %s\n", Arrays.toString (b));
077                reverseI (a);
078                StdOut.format ("reverse: %s\n", Arrays.toString (a));
079        }
080        public static void main (String[] args) {
081                test(new double[] { 10, 20, 60, 40, 50 });
082                test(new double[] { 60, 20, 30, 40 });
083                test(new double[] { 10, 20, 60 });
084                test(new double[] { 10, 20 });
085                test(new double[] { 10 });
086                test(new double[] { });
087        }
088}