Section Problems Number 2 - Array problems

Be prepared to discuss these problems in discussion section.

1. Describe what does the following method does?

`// g != null, n >= 0`

public static int[] r(int[] g, int n)

{ assert g != null && n >= 0 : "Violation of precondition, method
r";

int[] t = new int[n];

int m = (g.length < n) ? g.length : n;

for(int i = 0; i < m; i++)

{ t[i] = g[i];

}

return t;

}

2. Given method r what are the contents of arrays t2, t3, and t4 after the following code finished executing?

`int[] t1 = {1, 2, 3, 4};`

int[] t2 = r(t1, 7};

int[] t3 = r(t1, 2};

int[] t4 = r(t1, 0);

3. The following code times how long it takes for method r to complete when the
initial length of the array is 1,000,000 and the parameter n to method r is
2,000,000. Stopwatch is a non standard Java class we will be using throughout
the term. Here is a link to the
Stopwatch class source code
and a link to its
documentation.

`Stopwatch s = new Stopwatch();`

int size = 1000000;

int[] n = new int[size];

s.start(); //start timing

n = r(n, n.length * 2);

s.stop(); //stop timing

System.out.println(s);

On my computer I got the following output.

`elapsed time: 0.03 seconds.`

I ran the same code 100 times and got an average elapsed time of 0.031 seconds

I changed the initial length of the array to 2,000,000 and the parameter n to method r is 4,000,000 and got the following output.

`elapse time: 0.06 seconds.`

I ran the same code (with the initial length 2,000,000 and r set to 4,000,000) 100 times and got an average elapsed time of 0.056 seconds

If the initial length of the array is 100 instead of 1,000,000 and the parameter n to method r is still 2,000,000 what do you expect the elapsed time to be?

If the initial length of the array is 4,000,000 instead of 1,000,000 and the parameter n to method r is 8,000,000 what do you expect the elapsed time to be?

If the initial length of the array is 1,000,000 and the parameter n to method r is 100 what do you expect the elapsed time to be?

4. Given the following code that uses method r:

`int[] t1 = new int[1000];`

// .. various operations on t1

t1 = r(t1, t1.length * 3);

Does the following code accomplish the same thing as the code shown above?

`int[] t1 = new int[1000];`

// .. various operations on t1

int[] t2 = new int[t1.length * 3];

System.arraycopy(t1, 0, t2, 0, t1.length);

t1 = t2;

5. Write a method that accepts two arguments, an array of
integers, and a target number. The method shall print out all combinations of
exactly three values in the array that when added together equal the target
number. Print out the index and values for each combination. Consider this
example. The array consists of the following values {1, 3, 5, 8, 10, -2, 5, 0}
and the target is 13.

`Target: 13`

Combinations:

Indices: 1 2 6 Values: 3 5 5

Indices: 1 4 7 Values: 3 10 0

Indices: 2 3 7 Values: 5 8 0

Indices: 2 4 5 Values: 5 10 -2

Indices: 3 6 7 Values: 8 5 0

Indices: 4 5 6 Values: 10 -2 5

Is there any aspect of the problem that could be more generalized? How would you change your code to handle this more general problem?