|
|
Module 6: Arrays 39
To calculate result in a method and return it
1. Declare a new static method called Mully inside the MatrixMully
class. This method will return an int array of rank 2 and will expect two int
arrays of rank 2, named a and b, as parameters.
2. Copy (but do not cut) the declaration and initialization of result from Main
into Mully.
3. Add a return statement to Mully that returns result.
4. Replace the initialization of result in Main with a call to Mully, passing
a and b as arguments.
Your completed code should look as follows:
static int[,] Mully(int[,] a, int [,] b)
{
int[,] result = new int[2,2];
result[0,0]=a[0,0]*b[0,0] + a[0,1]*b[1,0];
result[0,1]=a[0,0]*b[0,1] + a[0,1]*b[1,1];
result[1,0]=a[1,0]*b[0,0] + a[1,1]*b[1,0];
result[1,1]=a[1,0]*b[0,1] + a[1,1]*b[1,1];
return result;
}
5. Compile the program, and correct any errors. Run the program. Verify that
the four values written to the console are still as follows:
19 22
43 50
To calculate result in a method by using for statements
1. Replace the initialization of result in Mully with a newly created 2 x 2
array of ints.
2. Add two nested for statements to Mully. Use an integer called r in the
outer for statement to it erate through each index of the first dimension of
result . Use an integer called c in the inner for statement to iterate through
each index of the second dimension of result. Use the literal value 2 directly
in both array bounds checks. The body of the inner for statement will need
to calculate and set the value of result[r,c] by using the following
formula:
result[r,c] = a[r,0] * b[0,c]
+ a[r,1] * b[1,c]
Your completed code should look like this:
static int[,] Mully(int[,] a, int [,] b)
{
int[,] result = new int[2,2];
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 2; c++) {
result[r,c] += a[r,0] * b[0,c] + a[r,1] * b[1,c];
}
}
return result;
}
40 Module 6: Arrays
3. Compile the program and correct any errors. Run the program. Verify that
the four values written to the console are still as follows:
19 22
43 50
To input the first matrix from the console
1. Replace the initialization of a in Main with a newly created 2 x 2 array of
ints.
2. Add statements to Main that prompt the user and read four values into a
from the console. These statements should be placed before invoking the
Mully method. The statements to read one value from the console are:
string s = Console.ReadLine( );
a[0,0] = int.Parse(s);
3. Compile the program and correct any errors. Run the program, entering the
same four values for a from the console (that is, 1, 2, 3, and 4). Verify that
the four values written to the console are still as follows:
19 22
43 50
4. Declare a new static method called Input inside the MatrixMully class.
This method will not return anything and will expect an int array of rank 2
as a parameter called a.
5. Cut the statements that read four values into a from Main and paste them
into Input. Add a statement to Main that calls Input, passing in a as the
parameter. This should be placed before the call to Mully.
6. Compile the program and correct any errors. Run the program, entering the
same four values for a from the console (that is, 1, 2, 3, and 4). Verify that
the four values written to the console are still as follows:
19 22
43 50
7. Change the Input method to use two nested for statements. Use the literal
value 2 directly in both array bounds checks. Include a Write statement
inside the Input method that prompts the user for each input. |
|