Research on The C Programming Language program [continuous update]

Source: Internet
Author: User

Chapter 1 Matrix 1. Transpose Problem description: Write a function to convert a given two-dimensional integer matrix to its transpose matrix. Input: 1 2 3 4 5 6 Output: 1 4 2 5 3 6 Analysis question requires writing a function that can transpose any two-dimensional matrix. Obviously, this function requires a parameter of a two-dimensional array, there should also be a two-dimensional array as the output. We can pass this array to the function, then the subscript of the function exchange row and column is assigned to the output array (B [j] [I] = a [I] [j. This seems to solve the problem. However, when a two-dimensional array is used as a parameter, the number of columns must be specified, that is, the size of the Second-dimensional array, for example, void transpose (int a [] [3], int B [] [2]), then this function can only transpose = to a matrix of 2 rows and 3 columns, which cannot meet the requirement of converting the question to any matrix. How can we make functions universal? We consider that the storage of arrays in the memory is linearly arranged in a row, for example, array a [2] [3] = {1, 2, 3}, {4, 5, 6 }}; B [3] [2] = {1, 2}, {3, 4}, {5, 6}; the layout of the two arrays is different, but they are arranged in the same way in the memory: a: a [0] [0] a [0] [1] a [0] [2] a [1] [0] a [1] [1] a [1] [2] 123456 B: B [0] [0] B [0] [1] B [1] [0] B [1] [1] B [2] [0] B [2] [1] In 123456, in some cases, a one-dimensional array can replace a two-dimensional array. The most important thing is that when a one-dimensional array is passed as a parameter to a function, you do not need to specify the length. This exactly matches the meaning of the question. Can we use a one-dimensional array to fulfill the requirements of the question? First, let's take a look at how transpose occurs in a two-dimensional array: 1 2 34 5 6 transpose → 1 42 53 6 obviously, in a two-dimensional perspective, the transpose of the matrix is to exchange row and column labels, that is, B [j] [I] = a [I] [j], the element a [I] [j] in the two-dimensional array of 2x3 is changed to a [I * 3 + j] from the one-dimensional perspective. the element B [j] [I] corresponding to the transposed 3X2 two-dimensional array is changed to B [j * 2 + I]. So that a [I * 3 + j] = B [j * 2 + I] can be transposed. So far, we have found the method of using a one-dimensional array to implement two-dimensional array transpose. Now let's look at how to convert a two-dimensional array into a one-dimensional array parameter. Because a one-dimensional array is a pointer to the int type when used as a function parameter, and a two-dimensional array name a is a pointer to an array, * a is a one-dimensional array, when using it as a function parameter, it is also a pointer to the int type. The following is a complete program:

/** General functions of the transpose matrix */void transpose (int * m1, int * m2, int x, int y ); // transpose function static int m2 [3] [2]; // automatically initializes int main (void) {int m1 [2] [3] = {1, 2, 3}, {4, 5, 6},}; int I; int j; transpose (* m1, * m2, 2, 3 ); // use the * m1, * m2 for (I = 0; I <3; I ++) {for (j = 0; j <2; j ++) Parameters) printf ("% 3d", m2 [I] [j]); printf ("\ n");} return 0;} void transpose (int * m1, int * m2, int x, int y) {int I; int j; for (I = 0; I <x; I ++) for (j = 0; j <y; j ++) m2 [j * x + I] = m1 [I * y + j];}

 

Summary: with the memory distribution of two-dimensional arrays, one-dimensional arrays can be used to deal with the problem of two-dimensional arrays, and this method is also universal. Ii. Matrix Multiplication Problem description: compile a function to implement the multiplication of two Arbitrary Matrices (the number of rows in one of the two matrices is the same as the number of columns in the other) input: m1: 2-6 3 5 1-1 m2: 4-2-4-5-7-3 6 7 output: r: 50 14-44-52-23-21 18 20 11 1-10-12 Analysis: We first consider the matrix multiplication rule: In the above example, there are obviously: r [0] [0] = m1 [0] [0] * m2 [0] [0] + m1 [0] [1] * m2 [1] [0]; r [0] [1] = m1 [0] [0] * m2 [0] [1] + m1 [0] [1] * m2 [1] [1]; ...... it is observed that in the equation above, all m1 row labels are consistent with r Row labels, and all m2 column labels are consistent with r column labels, the uncertain subscripts left in m1 and m2 are all traversed. What's more, the traversal ranges of the Two uncertain subscripts are consistent. Assume that m1 is x * y, m2 is y * z, m3 is x * z, And I, j, and k are used to traverse x, y, and z respectively. Then, for an element r [I] [k] (assuming that the initial values of all elements in r are 0), in the expression for calculating this element, all m1 row labels have been identified as I, and all m2 column labels are also determined as k. In this case, you only need to use j to traverse y, j is used as the undetermined value in m1 and m2, and I and k can be traversed to traverse all the r elements. The pseudo code is as follows: I: 0 → x k: 0 → z j: 0 → y r [I] [k] + = m1 [I] [j] * m2 [j] [k]; finally, we still need to use a one-dimensional array to solve the universal problem: m1 [I] [j] = m1 [I * y + j]; m2 [j] [k] = m2 [j * z + k]; r [I] [k] = r [I * z + k]; the final function and test procedure are as follows:
/** Matrix multiplication */# include <stdio. h> void multiply (int * m1, int * m2, int * r, int x, int y, int z); static int r [3] [4]; int main (void) {int m1 [3] [2] = {2,-6}, {3, 5}, {1,-1 }}; int m2 [2] [4] = {4,-2,-4,-5}, {-7,-3, 6, 7}; int I, j; multiply (* m1, * m2, * r, 3, 2, 4); for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) printf ("% 5d", r [I] [j]); printf ("\ n ");} return 0;} void multiply (int * m1, int * m2, int * r, int x, int y, int z) {int I, j, k; for (I = 0; I <x; I ++) for (j = 0; j <y; j ++) for (k = 0; k <z; k ++) r [I * z + k] + = m1 [I * y + j] * m2 [j * z + k];}

 

/** Test Result: ** 50 14-44-52 *-23-21 18 20*11 1-10-12 **/conclusion: analyze the multiplication rule, obtain the relationship between the following values.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.