Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active October 4, 2017 05:39
Show Gist options
  • Save dulimarta/6c56190b746efdb500f7316546142e14 to your computer and use it in GitHub Desktop.
Save dulimarta/6c56190b746efdb500f7316546142e14 to your computer and use it in GitHub Desktop.
CIS163 Lab: 2D Arrays
package cs163.arrays2d;
/**
* Created by Hans Dulimarta (Fall 2017).
*/
public class TwoDimensionalArrays {
/**
* Given a 2D array of integers, sumOfOdd() returns the sum of all the odd
* numbers in the array
* @param nums input array
* @return the sum of all the odd numbers
*/
public static int sumOfOdd(int[][] nums) {
return 0;
}
/**
* Given a 2D array of Strings, concatAll() returns the concatenation of
* all the elements in the array, separated by a hyphen (-).
* For instance, if the 2D array contains {{"What", "a"}, {"wonderful", "world"}}
* the method retuns "What-a-wonderful-world"
* @param words
* @return
*/
public static String concatAll (String[][] words) {
return null;
}
/**
* Given a 2D array of numeric grade, convertGrade() returns a 2D array of
* characters (the same size as the input array) representing the letter
* grade equivalent of each numeric grade according to these rules:
* 94 or above A
* 84 or above B
* 74 or above C
* 60 or above D
* otherwise F
*
* Hint you must first create the 2D array characters using the dimension
* of the input 2D array.
*
* @param numericGrade input numeric grades
* @return letter grade equivalent
*/
public static char[][] convertGrade(int[][] numericGrade) {
return null;
}
/**
* Given a 2D array of ints, columnTotal calculates the sum of each column
* and place each sum in a 1D array whose size is the same as the width
* of the 2D array. For instance, if the input 2D array is
* 9 4
* 2 1
* 8 0
*
* The method returns a 1D array {19, 5}
*
* @param nums input numbers
* @return 1D array containing the sum of each column in the input 2D array
*/
public static int[] columnTotal (int[][] nums) {
return null;
}
/**
* increasingRow() checks if every row in a 2D array contains strictly
* increasing values. For instance,
* For the 2D array
* 10 20 21 27
* 2 3 10 41
* the method returns true.
*
* For the 2D array
* 88 89 93 97 99
* 10 10 21 27 29
* 30 36 39 41 55
* the method returns false
*
* And for the following array (of width 1)
* 1
* 40
* 12
* 10
* the method returns true
*
* @param nums
* @return
*/
public static boolean increasingRow (int[][] nums) {
return false;
}
/**
* Given a 2D array, the following method updates the input array so
* the sume of each row is placed on the leftmost cell, and the remaining cells
* on the row is reset to zero.
* For instance, if the input array is
* 10 5 7 11 30
* -2 3 10 22 9
* 1 1 6 8 21
*
* the contents of the array are updated to
* 63 0 0 0 0
* 42 0 0 0 0
* 37 0 0 0 0
* @param nums
*/
public static void rowTotalLeft(int[][] nums) {
}
/**
* The following method compare the sum of all the numbers in the left
* half or a 2D array with the sum of its right half, and return
* -1 when the left half sum is smaller
* 0 when both sums are equal
* +1 when the left half sum is larger
*
* For instance, for the array
* 1 5 6 0
* 2 2 3 7
* 9 1 5 3
* the left half sum is 20 (1+5+2+2+9+1) and the right half sum is 24.
* And the method shall return -1.
*
* When the 2D array has an odd number of columns, the center column
* is included for both left and right sum. For instance
* 1 5 6 0 8
* 2 2 3 7 3
* the left half sum is 19 (1+5+6+2+3) and the right half sum is 27
*
* @param nums incoming array
* @return -1, 0, or +1
*/
public static int compareLeftRightHalf(int[][] nums) {
return 0;
}
/**
* This method scans and replicates any non-zero entries to its zero value
* left and right neighbors (whenever possible)
* When the 2D array initially contains the following values:
*
* 0 3 0 0 0 2 0 0 1 0
* 0 0 0 0 0 0 0 0 0 5
* 4 0 0 0 3 0 0 0 0 0
*
* the method updates the above array to:
*
* 3 3 3 0 2 2 2 1 1 1
* 0 0 0 0 0 0 0 0 5 5
* 4 4 0 3 3 3 0 0 0 0
*
* (you may assume that the initial contents are sparse enough, so copying
* the value to its neighbors will not create any conflicts).
*
* Warning: updating the array while scanning for non zero will
* affect outcome of the subsequent cycles of the scanning pass. To
* avoid this problem you may have to use an auxiliary array to hold
* the results of your scanning pass, and update the array on the second
* pass
*
* @param nums
*/
public static void replicateToLeftRightCells(int [][] nums) {
}
/**
* Similar to the previous method, but this method copies the non-zero
* entries to their NW, NE, SE, SW neighbors (whenever possible).
* When the 2D array initially contains the following values:
*
* 0 3 0 0 0 2 0 0 1 0
* 0 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 6 0 0
* 4 0 0 0 3 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0 0
*
* the method updates the array contents to:
* 0 3 0 0 0 2 0 0 1 0
* 3 0 3 0 2 0 2 1 0 1
* 0 0 0 0 0 0 6 0 6 0
* 0 4 0 3 0 3 0 6 0 0
* 4 0 0 0 3 0 6 0 6 0
* 0 4 0 3 0 3 0 0 0 0
* @param nums
*/
public static void replicateDiagonals(int[][] nums) {
}
/**
* This method scans for all non-zero entries in a 2D array, and adds
* 1 to its surrounding neighbors. However, instead of updating the incoming
* 2D array, this method performs the addition into a newly created array.
* For instance, when the incoming array
* is (3x6) 000000 it creates a new 3x6 000111
* 000030 110101
* 800000 010111
*
* incoming (4x7) new array (4x7)
* +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
* |0|0|0|0|8|0|0| |0|0|0|1|0|1|0|
* +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
* |0|0|0|0|0|0|0| |0|1|1|2|1|1|0|
* +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
* |0|0|7|0|0|0|0| |0|1|0|1|0|0|0|
* +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
* |0|0|0|0|0|0\0| |0|1|1|1|0|0|0|
* +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
*
* @param nums
*/
public static int[][] infectAllNeighbors(final int[][] nums) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment