回分類題庫
d199: CP Lab Exercise: Matrix Multiplication
出處:

Difficulity : N/A
Accepted : 49 Times | Submit :98 Times | Clicks : 1402
Accepted : 34 Users | Submit : 38 Users | Accepted rate : 89%
Time Limit :10000 ms | Memory Limit : 99999 KBytes
題目加入時間 : 2010-12-29 15:24

Content :

Back to high school math again, matrix multiplication is important all the time. When we scale, rotate, or translate coordinates, we need to use matrix multiplication.

Matrix multiplication is also important to computer graphic, such as Open GL, DirectX…etc.

PS: Matrix is not the famous movie.

You are asked to write a matrix multiplication program based on dotProduct function in exercise 1.

In this exercise, you need to dynamic allocate 2D-arrays for matrix and free them when they are useless.

Fill your dotProduct function in the given code, ~g9803/matrixMul_ex.c

Implement all the functions:
// From exercise 1
int dotProduct(int *first, int *second, int size);

// Allocate a 2D array with size row * column
int** createMatrix(int row, int column, int init);

// Free a 2D array with specific row
void deleteMatrix(int **matrix, int row);

// Matrix Multiplication of matrix1(m*n) and matrix2(n*r)
// and store the result in result.
void matrixMul(int **matrix1, int **matrix2, int **result, int m, int n, int r);

Some implemented functions:
int* getMatrixRow(int **matrix, int kRow, int rowSize);
Get a specific row from a matrix, rowSize is the length of the specific row.

int* getMatrixCol(int **matrix, int kCol, int colSize);
Get a specific column from a matrix, colSize is the length of the specific column.

Notice that:
    You need to dynamic allocate a 2D array in createMatrix function.
    You need to free a 2D array in deleteMatrix function.
    You need to use dotProduct in matrixMul function.
        getMatrixRow and getMatrixCol can be used here.
    All the function prototypes shall not be modified.

Sample Program: ~g9803/matrixMul
 

Input :

There are several sets of data, for each data, the first line contains three integers M, N, R which is the matrix's size.
The following M lines are the first matrix, each line contains N integers.
The last N lines are the second matrix, each line contains R integers.

Output :

For each set of data, output the M lines, each lines has R integers according to the matrix.

Sample Input :

2 3 41 2 3
4 5 6
1 2 3 4
5 6 7 8
9 10 11 12
2 2 2
1 2
1 2
1 2
1 2

Sample Output :

38 44 50 56
83 98 113 128
3 6
3 6

Hint :


  

Author :


  Solve it!   Status Forum (0)

C++
C
JAVA
49523. CC501 (170 ms , 392KB)
30704. nothinglo (132 ms , 308KB)
31055. cp99703032 (138 ms , 300KB)
31559. cp99703045 (140 ms , 534KB)
31065. cp99703003 (142 ms , 306KB)
31561. cp99703018 (144 ms , 568KB)
58304. nwgs524513cja (570 ms , 23216KB)
57917. nwgs524513cja (696 ms , 25950KB)

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡