1. Write a Method That Finds the Average of All Elements in a Matrix, I.E., A

Comp 110

Algorithms & Programming

Take Home

Midterm Examination #2

9 December 2007

Name:______Answers______

1.  Write a method that finds the average of all elements in a matrix, i.e., a

2-dimensional array, of integers. The method should accept matrices of all sizes, e.g., the following examples:

4x4 matrix 4x3 matrix 3x4 matrix

11 29 13 94 65 78 23 43 48 16 86

76 17 18 29 21 34 87 61 39 63 27

10 11 52 13 23 45 72 28 37 99 35

14  25 16 37 90 87 31

int avg_matix( int [ ][ ] m, int k; int l)

{

int sum = 0;

for( i = 0; i <= k; i++ )

for( j = 0; j <= l; j++ )

sum += m[ i ][ j ];

return sum;

}

2.  Write a method that multiplies two matrices. The header of the method is

public static int [ ] [ ] multiplyMatrix( int [ ] [ ] a, int [ ] [ ] b)

In order to multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b and the two matrices must have elements of compatible types.

Let matrix c be the result of multiplying matrix a by matrix b, i.e, c = a * b; e.g.,

a11 a12 a13 b11 b12 b13 c11 c12 c13

a21 a22 a23 ● b21 b22 b23 è c21 c22 c23 b31 b32 b33

where cij = ai1 * b1j + ai2 * b2j + ai3 * b3j

As a concrete example

1 2 3 7 8 9 1*7 + 2*1 + 3*6 1*8 + 2*4 + 3*8 1*9 + 2*3 + 3*4 27 40 27

4 5 6 ● 1 4 3 è 4*7 + 5*1 + 6*6 4*8 + 5*4 + 6*8 4*9 + 5*3 + 6*4 è 69 100 75

6 8 4

In order to construct the required method, with the knowledge covered in the lectures, the header must be expanded to include the dimensions of the matrices.

public static int [ ] [ ] multiplyMatrix( int [ ] [ ] a, int m, int n, int [ ] [ ] b, int p, int r)

{

if (n != p)

System.out.println(“Matrices are not compatible n, i.e., “+ n +“ != p, i.e., “+p);

int c[ m ][ r ];

for( i = 0; i <= m; i++ )

for ( j = 0; j <= n; j++ )

for ( k = 0; k <= r; k++ )

c[ i ][ k ] = a[ i ][ j ] * b[ j ][ k ];

return c;

}

3.  Design and implement the Fan class as specified in Liang age 256 problem # 7.2.

Submit the UML diagram, the source code for the Fan class, the test program, and the output for the two Fan objects.

public class Fan

{

final int SLOW = 1;

final int MEDIUM = 2;

final int FAST = 3;

int speed = SLOW;

boolean on = FALSE;

double radius = 5;

String color = “blue”;

Fan( ) { }

int get_speed ( )

{

return speed;

}

void set_speed ( int speed )

{

this.speed = speed;

}

boolean get_on ( )

{

return on;

}

void set_on (boolean on)

{

this.on = on;

}

double get_radius ( )

{

return radius;

}

void set_radius (double radius)

{

this.radius = radius;

}

String get_color ( )

{

return color;

}

void set_color (String color)

{

this.color = color;

}

String toString( )

{

if (on == TRUE )

return “speed = ”+ speed +“ color = ”+ color + “ radius = ” + radius;

else return “color = ”+ color + “ radius = ” + radius + “ FAN IS OFF”;

}

}


public static void main(String[ ] args)

{

Fan f1 = new fan( );

Fan f2 = new fan( );

f1.set_speed(FAST);

f1.set_radius(10);

f1.set_color(“yellow”);

f1.set_on(TRUE);

f2.set_speed(MEDIUM);

f2.set_radius(5);

f2.set_color(“blue”);

f2.set_on(FALSE);

System.out.println(f1.toString);

System.out.println(f2.toString);

}

5