CSCI A201/A597 and I210

Problem Set Five

Second semester 2000-2001


Your problem will be randomly distributed to you by logging in to QuizSite.

Due date is

Sat Mar 31 11:59pm
Solutions to be posted here the following day, at noon.

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


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.

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

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

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.

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.

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.


Last updated: Mar 8, 2000 by Adrian German for A201