CSCI A201/A597 and I210

Homework Five Solutions

Second semester 2000-2001


Date: Thu, 8 Mar 2001 11:52:09 -0500 (EST)
From: Adrian German <dgerman@cs.indiana.edu>
To: Andrey Salaev <asalaev@cs.indiana.edu>,
     Adrian German <dgerman@cs.indiana.edu>,
     Francisco Lara-Dammer <flaradam@cs.indiana.edu>, malani@indiana.edu,
     myadroff@indiana.edu, Alex Leykin <oleykin@cs.indiana.edu>,
     Robert Najlis <rnajlis@cs.indiana.edu>, sapark@indiana.edu,
     sgrogg@indiana.edu, vpavlor@indiana.edu, Ying Jin <yinjin@cs.indiana.edu>,
     Zhen Pan <zhpan@cs.indiana.edu>
Subject: project 5 key

Dear friends,

I posted the fifth project a few hours ago. It's already uploaded
in QuizSite. Here is the key. Please use this as just a possible way
in which one can solve the problems. There could be other ways of
solving these problems. 95 remains the highest A. (A's are in the
range of 90-95, A+ is 96-100).

--------------------------------------------------------------------------

1. Write a method

           static double scalarProduct(double[] a, double[] b)

that computes the scalar product of two mathematical vectors
(represented as arrays). The scalar product is:

           a0b0 + a1b1 + ... + an-1bn-1

Here's the solution:

       public static double scalarProduct (double[] a, double[] b) {
         double result = 0;
         for (int i = 0; i < a.length; i++)
           result += a[i] * b[i];
         return result;
       }

---------------------------------------------------------------------------

2. Write a method that computes the alternating sum of all elements in an
array. For example, if alternatingSum is called with an array containing

           1   4   9   16   9   7   4   9   11

Then it computes

           1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11

which is, of course, -2.

Here's the solution:

       public static double alternatingSum(double[] a) {
         if (a.length == 0) return 0;
         double value = 0;
         for (int i = 0; i < a.length; i++) {
           if (i % 2 == 0) value += a[i];
           else value -= a[i];
         }
         return value;
       }

----------------------------------------------------------------------------

3. Write a method reverse that reverses the sequence of elements in an
array. For example, if reverse is called with an array containing

           1   4   9   16   9   7   4   9   11

then the array is changed to

           11   9   4   7   9   16   9   4   1

Here's the solution:

       public static void reverse(double[] a) {
         for (int i = 0; i < a.length / 2; i++) {
           double temp = a[i];
           a[i] = a[a.length - i - 1];
           a[a.length - i - 1] = temp;
         }
       }

---------------------------------------------------------------------------

4. Write a method

           public static int[] append(int[] a, int[] b)

that appends one array after another. For example, if a is

           1   4   9   16

and b is

           9   7   4   9   11

then append returns the array

           1   4   9   16   9   7   4   9   11

Here's a solution:

       public static int[] append(int[] a, int[] b) {
         int[] result = new int[a.length + b.length];
         for (int i = 0; i < a.length; i++)
           result[i] = a[i];
         for (int i = 0; i < b.length; i++)
           result[i + a.length] = b[i];
         return result;
       }

--------------------------------------------------------------------------

5. Write a predicate method

           public static boolean equals(int[] a, int[] b)

that checks whether two arrays have the same elements in the same order.

Here's a solution:

       public static boolean equals(int[] a, int[] b) {
         if (a.length != b.length)
           return false;
         for (int i = 0; i < a.length; i++)
           if (a[i] != b[i])
             return false;
         return true;
       }

--------------------------------------------------------------------------

6. Write a predicate method

           public static boolean sameSet(int[] a, int[] b)

that checks whether two arrays have the same elements in some order,
ignoring multiplicities. For example, the two arrays

           1   4   9   16   9   7   4   9   11

    and

           11   11   7   9   16   4   1

would be considered to have the same set. You will probably need one
or more helper methods.

Here's a solution:

       public static boolean sameSet(int[] a, int[] b) {
         for (int i = 0; i < a.length; i++)
           if (! contains(b, a[i]))
             return false;
         for (int i = 0; i < b.length; i++)
           if (! contains(a, b[i]))
             return false;
         return true;
       }

------------------------------------------------------------------------

7. Write a predicate method

           public static boolean sameElements(int[] a, int[] b)

that checks whether two arrays have the same elements in some order,
with the same multiplicities. For example,

           1   4   9   16   9   7   4   9   11

and

           11   1   4   9   16   9   7   4   9

would be considered to have the same elements, but

           1   4   9   16   9   7   4   9   11

and

           11   11   7   9   16   4   1

would not.

You will probably need one or more helper methods.


Here's a solution:

       public static boolean sameElements(int[] a, int[] b) {
         for (int i = 0; i < a.length; i++)
           if (count(a, a[i]) != count(b, a[i]))
             return false;
         for (int i = 0; i < b.length; i++)
           if (count(a, b[i]) != count(b, b[i]))
             return false;
         return true;
       }

------------------------------------------------------------------------

It will be up to you to come up with a grading scale. Of course, your
main responsibility is to teach the students the concepts in the lab,
or during office hours, well enough that they can solve these problems.

If you are successful then everybody will do well. If that's not the
case you will have "failed" in some respects. Count these ways, and
take points off for things you said in class, or showed in lab or
during office hours, that could have helped with the assignments,
and that you could not find in the assignments, when turned in. And
that would be the grade for the assignment, on a scale of 0-100.

I post lab notes with every lab. Please feel free to post your own
set of notes (I could help you with that if you want me to), and feel
free to turn the lab into an extended presentation on the projector if
you want to show the students programs, exercises that you'd be putting
together for them as examples, to prepare them for the homework
assignment.

If you have questions or concerns please let me know.

I appreciate all your help.

... Adrian

Last updated: April 3, 2001 by Adrian German for A201