import java.util.*;
/*
* Source: https://apcomputersciencetutoring.com/exam-review/determinantfinder-practice-problem/
*/
public class TwoDArrayPractice
{
public TwoDArrayPractice(){
int[][] testArr = {
{10, 9, 8, 7},
{6, 5, 4, 3},
{2, 1, -1, 0},
{22, 31, 11, 40}
};
String phrase = "stop whining";
int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
invert(testArr);
/*
void rowSwap(int[][] mat, int rowAIndex, int rowBIndex);
colSwap(int[][] mat, int colAIndex, int colBIndex);
String[][] k = fillRowMajor(String str, int rows, int cols);
int[][] j = fillColumnMajor(int[] vals, int rows, int cols);
int[][] fdu = fillDownUp(int[] vals, int rows, int cols);
int[][] gr = grow(int[][] mat, int newRows, int newCols);
int[][] cr = crop(int[][] mat, int startRow, int startCol, int endRow, int endCol);
int[][] inv = invert(int[][] mat);
*/
}
/**
* Swaps all values in the specified 2 rows of mat.
* @param mat the array
* @param rowAIndex the index of a row to be swapped
* @param rowBIndex the index of a row to be swapped
int[][] testArr = {
{10, 9, 8, 7},
{6, 5, 4, 3},
{2, 1, -1, 0}
{22, 31, 11, 40}
};
*/
public static void rowSwap(int[][] mat, int rowAIndex, int rowBIndex)
{
rowAIndex --;
rowBIndex --;
for(int i = 0; i < mat[0].length; i++){
int temp = mat[rowAIndex][i];
mat[rowBIndex][i] = temp;
mat[rowAIndex][i] = mat[rowBIndex][i];
}
}
/**
* Swaps all values in the specified 2 columns of mat.
* @param mat the array
* @param colAIndex the index of a column to be swapped
* @param colBIndex the index of a column to be swapped
*/
public static void colSwap(int[][] mat, int colAIndex, int colBIndex)
{
colAIndex --;
colBIndex --;
for(int i = 0; i < mat.length; i++){
int temp = mat[i][colAIndex];
mat[i][colAIndex] = mat[i][colBIndex];
mat[i][colBIndex] = temp;
}
}
/**
* Returns an array with the specified number of rows and columns
* containing the characters from str in row-major order. If str.length()
* is greater than rows * cols, extra characters are ignored. If
* str.length() is less than rows * cols, the remaining elements in the
* returned array contain null.
String phrase = "stop whining";
int numRows = 3, numCols = 4;
String[][] expectedResult = {
{"s", "t", "o", "p"},
{" ", "w", "h", "i"},
{"n", "i", "n", "g"}
};
*
* @param str the string to be placed in an array
* @param rows the number of rows in the array to be returned
* @param cols the number of columsn in the array to be returned
* @return an array containing the characters from str in row-major order
*/
public static String[][] fillRowMajor(String str, int rows, int cols)
{
int spot = cols * rows;
int length = str.length();
for(int i = 0; i <= (spot - length); i++){
str += "";
}
String[][] result = new String[rows][cols];
for(int r = 0; r < result.length; r++){
for(int c = 0; c < result[r].length; c++){
result[r][c] = str.substring(0,1);
str = str.substring(1);
}
}
return result;
}
/**
* Returns an array containing the elements of vals in column-major order.
*
* Precondition: vals.length == rows * cols
*
* @param vals the elements
* @param rows the number of rows in the array to be returned
* @param cols the number of columns in the array to be returned
* @return an array containing the elements of vals in column-major order
*/
public static int[][] fillColumnMajor(int[] vals, int rows, int cols)
{
int counter = 0;
int[][] constant = new int[rows][cols];
for(int i = 0; i < constant[0].length; i++){
for(int p = 0; p < constant.length; p++){
constant[p][i] = vals[counter];
counter ++;
}
}
//System.out.println(Arrays.deepToString(constant));
return constant;
}
/**
* Returns an array with the specified number of rows and columns that
* contains the elements of vals in the order specified below. Elements
* from vals are placed in the array by moving down the first column,
* up the second column and so on.
*
* For example, if vals was:
* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
*
* rows was 3 and cols was 4,
* the returned array would be:
* { {1, 6, 7, 12},
* {2, 5, 8, 11},
* {3, 4, 9, 10} }
*
* Precondition: vals.length == rows * cols
*
* @param vals the elements
* @param rows the number of rows in the array to be returned
* @param cols the number of columns in the array to be returned
* @return an array containing the elements from vals in the specified order
*/
public static int[][] fillDownUp(int[] vals, int rows, int cols)
{
int counter = 0;
int[][] ordered = new int[rows][cols];
for(int i = 0; i < ordered[0].length; i++){
if(i % 2 == 0){
for(int p = 0; p < ordered.length; p++){
ordered[p][i] = vals[counter];
counter ++;
}
}else if(i % 2 > 0){
for(int p = ordered.length - 1; p >= 0; p--){
ordered[p][i] = vals[counter];
counter ++;
}
}
}
//System.out.println(Arrays.deepToString(ordered));
return ordered;
}
/**
* Returns a larger array containing the same elements as the
* mat. The elements from mat are read in row-major order and
* appear in the new array in row-major order.
*
* For example, if mat was:
* { {10, 9, 8, 7},
* {6, 5, 4, 3},
* {2, 1, -1, 0} }
*
* newRows was 4 and newCols was 5
* the returned array would be:
* { {10, 9, 8, 7, 6},
* {5, 4, 3, 2, 1},
* {-1, 0, 0, 0, 0},
* {0, 0, 0, 0, 0} }
*
* Precondition: newRows > mat.length && newCols > mat[0].length
*
* @param mat the 2D array containing the original elements
* @param newRows the number of rows in the new array
* @param newCols the number of columns in the new array
* @return a larger array containing the elements from mat in row-major order
*/
public static int[][] grow(int[][] mat, int newRows, int newCols)
{
int original = 0;
int[] array = new int[(newRows * newCols)];
for(int i = 0; i < mat.length; i++){
for(int c = 0; c < mat[i].length; c++){
array[original] = mat[i][c];
original++;
}
}
for(int i = original; i < array.length; i++){
array[original] = 0;
}
int originalTwo = 0;
int[][] grow = new int[newRows][newCols];
for(int r = 0; r < grow.length; r++){
for(int p = 0; p < grow[r].length; p++){
grow[r][p] = array[originalTwo];
originalTwo++;
}
}
//System.out.println(Arrays.deepToString(grow));
return grow;
}
/**
* Returns a smaller array containing the elements in the specified
* range of the mat.
*
* For example,
*
* mat:
* { {10, 9, 8, 7},
* {6, 5, 4, 3},
* {2, 1, -1, 0} }
*
* startRow: 0, startCol: 1, endRow: 1, endCol: 2
*
* would yield:
* { {9, 8},
* {5, 4} }
*
* @param mat the 2D array containing the original elements
* @param startRow the first row to be kept
* @param startCol the first column to be kept
* @param endRow the last row to be kept
* @param endCol the last column to be kept
* @return a smaller array containing the specified elements
*/
public static int[][] crop(int[][] mat,
int startRow, int startCol,
int endRow, int endCol)
{
int rowsNeeded = endRow - startRow + 1;
int colsNeeded = endCol - startCol + 1;
int[][] small = new int[rowsNeeded][colsNeeded];
int[] big = new int[rowsNeeded * colsNeeded];
int spot = 0;
for(int r = startRow; r <= endRow; r++){
for(int c = startCol; c <= endCol; c++){
big[spot] = mat[r][c];
spot++;
}
}
int spot2 = 0;
for(int r = 0; r < small.length; r++){
for(int c = 0; c < small[r].length; c++){
small[r][c] = big[spot2];
spot2++;
}
}
//System.out.println(Arrays.deepToString(small));
return small;
}
/**
* Returns an array containing the same elements as
* mat but with the rows and columns reversed.
*
* For example:
*
* mat:
* { {10, 9, 8, 7},
* {6, 5, 4, 3},
* {2, 1, -1, 0} }
*
* would yield:
* { {10, 6, 2},
* {9, 5, 1},
* {8, 4, -1},
* {7, 3, 0} }
*
* @param mat the array
* @return an array containing elements as described
*/
public static int[][] invert(int[][] mat)
{
int[][] inverted = new int[mat[0].length][mat.length];
int[] all = new int[mat[0].length * mat.length];
int spot = 0;
for(int i = 0; i < mat[0].length; i++){
for(int r = 0; r < mat.length; r++){
all[spot] = mat[r][i];
spot++;
}
}
int spotTwo = 0;
for(int i = 0; i < inverted.length; i++){
for(int c = 0; c < inverted[0].length; c++){
inverted[i][c] = all[spotTwo];
spotTwo++;
}
}
//System.out.println(Arrays.deepToString(inverted));
return inverted;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.