CmSc150 Fundamentals of Computing I
Homework 10 due 03/31 name___________
The methods to be developed have to be placed in the Arrays class. Add corresponding
options in the driver class.
1. Implement a method that given two „int‟ arrays of size M and N with unique
elements in ascending order, merges the elements of the arrays in a third array so
that the elements remain in ascending order.
public static void merge(int arrayA, int arrayB, int arrayC)
Algorithm in pseudocode (begin-end is used to indicate loop body)
1. Initialize i, j, k to 0
2. while there are elements in arrayA and in arrayB do
if arrayA[i] < arrayB[j]
i i + 1
k k + 1
j j + 1
k k + 1
3. copy the remaining elements from arrayA to arrayC
4. copy the remaining elements from arrayB to arrayC
Note: Only one of the arrays will have remaining elements, so only one of steps 3
and 4 will be executed. To copy the remaining elements we need a for loop
without initializing the loop variable, i.e. its value is the value when exiting the
merging loop. The condition is “loop variable less than the size of the array”, thus
one of the loops will be skipped (its loop variable will be exactly the size of the
array) and the other will run. So we don‟t need to check which array has
remaining elements, the check will be done in the header of the corresponding
loop in steps 3 and 4.
Add a new option “merge” to the driver class and implement a corresponding case
that performs the actions necessary to accomplish the task.
Note: You need to create the two arrays in ascending order – use the method
developed in Lab11.
2. Implement a method that given an „int‟ array and integer number , stores in
another array all elements that are multiples of the given number, and returns the
number of elements stored in the new array. Add an option “multiple” in the
driver class to test the new method.
public static int findMultiples(intarrayA, int arrayB, int item)
Algorithm in pseudocode
Source array: arrayA
Target array: arrayB
Given number: item
1. Initialize variable k to 0
2. For all elements arrayA[i] do
if arrayA[i] is multiple of item
k k + 1
3. return k
Hint: to check for multiples use the % operator: if a is a multiple of b, the a % b is 0
3. Implement a method void printRight(int number) that given a number N prints the
1 2 3
1 2 3 … … N
Add this method to the Arrays class and add an option to test the method
Hint: You need to print spaces before printing the numbers. In the first row we need
2*(N-1) spaces before printing 1. In the second row we need 2*(N-2) spaces before
printing 1 and 2, etc.
Thus we need one loop to control printing the rows. (one run of this loop will print
one row). Inside that loop we can use two other loops
- to print the spaces
- to print the numbers
So the stricture will be:
// print spaces
// print numbers
Turn in: the zipped project (do not forget to change the extension to .zi). The project
name has to start with your name.