Assignment 3
2D Array Manipulation (Console Program)
Due Date: 4/12/2022 midnight
The purpose of
this program is to write a series of function to manipulate a two-dimension
array.
ArrayData method: It will allocate a
block of memory which represents a two-dimension array based on the input
values supplied by the user. Ask the
user to enter the number of rows and column for 2D array that the user want to
manipulate. The number of rows and
columns that the user enters may or may not define a square matrix (when the
number of rows equals the number of columns).
The array will have exactly rows time columns (m * n) elements. It will not contain any extra or empty cells. Initialize the “matrix” by rows with random
number between 1 to 100. Pass two
arguments by out reference so that you can assign the number of row and columns
of data to the first and second arguments. This is return 2D array method after
allocating new memory for 2D array and initialize it with random value. Generate random number code in C# as
following: Random randNum
= new Random( );
int num = randNum.Next
(1, 101);
SwapRow method: it will rotate
the data in the matrix in an up/down method such that the first row is
exchanged with last row (swap row 0 and row m-1, swap row 1 and row m-2, etc.).
SwapColumn method:
it
will rotate the data in the matrix in a left/right method such that the first
column is exchanged with the last column (swap column 0 and column n-1, swap
column1 and column n-2, etc.)
RotateLeftDiagonal
method:
it will rotate the data in the matrix by the left diagonal. This operation is usually referred to as a matrix
transpose. This is done by exchanging
element [i, j] with element [j, i]. The
values on the left diagonal do not move for a square matrix. Do not forget to swap the values for the
number of rows and columns. Pass these
two arguments by reference so that you can change their values.
RotateRightDiagonal
method:
it will rotate the data in the matrix by the right diagonal. This operation will require that you exchange
element [0, 0] with element [m-1, n-1].
The values on the right diagonal do not move for a square matrix. Do not forget to swap the values of the
number of rows and columns. Pass these
two arguments by reference so that you can change their values.
DisplayArray
method:
it will print the array in a two-dimension format with rows and columns as you
would normally print a compile time defined two-dimension array. Be sure that the columns are aligned
correctly for display purposes.
Use type integer
(int) for the array. In the main, program
call methods in following order: ArrayData, SwapRow, SwapColumn, RotateLeftDiagonal,
RotateRightDiagonal. After each method
has been called you must print the matrix, which is calling DisplayArray
method, and pause the program so that the result of the operation can be
viewed. After the results of the
operation of 4 methods has been printed, then continue running the program
until the user want to quit. Clearly
label each matrix with the operation that was performed on the matrix.
// IST 211 A3 note:
Main():
1. Declare variable for two-dimension array, row, colums
Example: int[,] twoDarray;
2. Use do…while to run the program until the user want to stop.
Example:
do
{
….
}while (condition check)
3. call ArrayData method with return two-dimension array.
Example: twoDarray = ArrayData(out int row, out int col);
4. write proper messge for print array data.
Example: “The Original Two Dimension Array Data:”
5. Call DisplayArray method to print two-dimension array data
Example: PrintArray(twoDarray, rows, columns);
6. Pause the program:
Example: ReadKey();
7. Call SwapRow method to flip the rows upside down.
Example: SwapRow(twoDarray, rows, columns);
8. Repeat step 4 ~ step 6
9. Call SwapColumn method to flip the column lef to right.
Example: SwapColumn(twoDrray, rows, columns);
10. Repeat step 4 ~ step 6
11. Call RotatsLeftDiagnoal method with return new two-dimension array.
Example: twoDarray = RotateLeftDiagnoal(twoDarray, ref rows, ref columns);
12. Repeat step 4 ~ step 6
}
Methods:
ArrayData Method:
static in[,] ArrayDate(out int row, out int col)
{
1. This method will ask the user to enter row and column size
for the two-dimension array.
2. Declared two-dimension array with the user input row size and olumn size.
3. Initialized two-dimension array data with random number between 1 to 100.
Single-dimension array with random number initialization example:
Random randNum = new Random();
int[] array = new int[size];
for(int x = 0; x < array.Length; ++x)
{
array[x] = randNum.next(1, 101); // random number is between 1 and 100
}
4. return two-dimension array
}
DisplayArray method:
static void DisplayArray(int[,] array, int row, int col)
{
print array data with two-dimension table like format
your need to figure out when to write("n"); statement in two-dimension array
when each row finished.
Example for single-dimension array:
for(int x = 0; x < array.Length; ++x)
{
Write("{0}t", array[x]);
}
}
SwapRow method:
static void SwapRow(int[,] array, int row, int col)
{
rotate the data in the two-dimension array in an up/down method such that
the first row is exchanged with last row (swap row 0 and row m-1,
swap row 1 and row m-2,....etc.) //m is row size
}
SwapColumn method:
static void SwapColumn(int[,] array, int row, int col)
{
rotate the data in the two-dimension array in a left/right method such that
the first column is exchanged with the last column (swap column 0 and column n-1,
swap column 1 and column n-2,....etc.) // n is columnsize
}
RotateLeftDiagonal method:
static int[,] RotateLeftDiagnoal(int[,] array, ref int row, ref int col)
{
1. check row size is same as column size, if their are not equal,
then swap row size with column size.
2. create new two-dimension array with current row and column size.
3. this operation is usually referred to as a array transpose.
This si donw by exchange old array element [i, j] with new array element [j, i].
The values on the left diagonal do not move for a square array.
4. return new two-dimension array.
}
2-dimension array index is row (i), column (j)
1 2 3 [0, 0] [0, 1] [0, 2]
4 5 6 => [1, 0] [1, 1] [1, 2]
7 8 9 [2, 0] [2, 1] [2, 2]
leftDiagonal
1 4 7 [0, 0] [1, 0] [2, 0]
2 5 8 => [0, 1] [1, 1] [2, 1]
3 6 9 [0, 2] [1, 2] [2, 2]
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
RotateRightDiagonal method:
static int[,] RotateRightDiagonal(int[,] array, ref int row, ref int col)
{
1. check row size is same as column size, if their are not equal then
swap row size column size.
2. create new two-dimension array with current row and column.
(if their are same size, you do not need to create new two-dimension array.)
3. This operation will require that you exhange row and column element [0, 0] with
new array element [m-1, n-1]. The values on the right diagonal do not move for
a square array.
4. return new two-dimension array.
}
Last Completed Projects
| topic title | academic level | Writer | delivered |
|---|
