Returns the largest two-dimensional matrix (element and maximum) in a matrix)

Source: Internet
Author: User

I. Problem Description

Question 35. Find the largest two-dimensional matrix (element and maximum) in a matrix)

For example:
1 2 0 3 4
2 3 4 5 1
1 1 5 3 0
The largest in:
4 5
5 3
Requirements: (1) write algorithms; (2) analyze time complexity; (3) use C to write key code

 

Ii. Algorithm Analysis

The solution to this problem is relatively simple: Calculate all possible two-dimensional matrix elements and select the largest one.

Detailed solution: Save the data of the original matrix in original_array [] []

1. Traverse original_array to add the value of column I and column I + 1 and save it in column I of another two-dimensional array new_array.

2. by traversing new_array, add the value of row I and row I + 1, and save the value in row I of new_array. At this time, new_array stores all elements of the two-dimensional matrix and

3. Traverse new_array to find the maximum value and its corresponding row and column to obtain the element and the largest two-dimensional matrix.

The time complexity and space complexity of this algorithm are both O (Mn). For the original match of n columns in m rows

 

Iii. Source Code

# Include <stdio. h> # include <stdlib. h> void display_array (INT ** array, int row, int col); void max_2order_sqare (INT ** array, int row, int Col, int max_sub_array [2] [2]); void ** malloc_array (INT row, int Col, int size); void max_2order_sqare (INT ** array, int row, int Col, int max_sub_array [2] [2]) {int result_row, result_col; // Save the maximum two-dimensional matrix and the starting row and column int ** new_array = (INT **) malloc_array (row, col-1, sizeof (INT ); // Apply for a two-dimensional array, row col-1 column int I, j; int Max; // Save the largest two-dimensional matrix and // Add the values of the adjacent two columns of the original matrix: the values of column I and column I + 1 of the original matrix are added and saved to column I of new_array. Therefore, the number of columns of new_array is one less column for (I = 0; I <row; I ++) {for (j = 0; j <col-1; j ++) {new_array [I] [J] = * (int *) array + I * Col + J) + * (int *) array + I * Col + J + 1); // the two-dimensional array value is transmitted through a two-dimensional pointer, therefore, you cannot reference the elements in the array using array [I] [J]} // initialize the [0] [0] -- [1] [1] of the original matrix] Max = new_array [0] [0] + new_array [1] [0]; result_row = 0; Result _ Col = 0;/*** Add the values of two adjacent lines of new_array: add the value of line I of new_array and the value of line I + 1, and save it to line I of new_array, therefore, the number of new_array rows is one column less ** after adding two adjacent rows, the elements in new_array are all two-dimensional matrix elements and, select the largest result */for (I = 0; I <row-1; I ++) {for (j = 0; j <col-1; j ++) {new_array [I] [J] = new_array [I] [J] + new_array [I + 1] [J]; if (new_array [I] [J]> MAX) {max = new_array [I] [J]; result_row = I; result_col = J ;}}} // display_array (INT **) new_array, 2, 4); DIS Play_array (INT **) (& new_array [0] [0]), row-1, col-1 ); // output all two-dimensional matrix elements and // The largest element and two-dimensional matrix have been found, save the content to max_sub_array [0] [0] = * (int *) array + result_row * Col + result_col ); max_sub_array [0] [1] = * (int *) array + result_row * Col + result_col + 1); max_sub_array [1] [0] = * (int *) array + (result_row + 1) * Col + result_col); max_sub_array [1] [1] = * (int *) array + (result_row + 1) * Col + result_col + 1); fre E (new_array); // After dynamically applying for a two-dimensional matrix, the memory needs to be released !!!} /*** Function: Format and output a two-dimensional array, which solves the problem that the rows and columns of a two-dimensional array do not need to be fixed. the rows and columns can be specified by parameters. ** input: pointer to the pointer (the name of the Two-dimensional array is forcibly converted by type conversion). The number of rows and columns of the Two-dimensional array ** output: none */void display_array (INT ** array, int row, int col) {int I, j; printf ("here is this % d * % d array's numbers: \ n", row, col); for (I = 0; I <row; I ++) {for (j = 0; j <Col; j ++) {printf ("% d", * (int *) array + I * Col + J); // no more elements in the array can be referenced through array [I] [J].} printf ("\ n ");}} /*** function: dynamically applies for two-dimensional arrays. features: the number of rows and columns are variable. It is very convenient to reference array elements. You need to apply for an additional Col pointer space. ** input: number of rows, columns, and space occupied by a single element of the Two-dimensional array to be applied ** output: returns a two-dimensional pointer pointing to the dynamically applied space, you can use array [I] [J] to reference the array element */void ** malloc_array (INT row, int Col, int size) {Int J; int indexsize; void ** array = NULL; char * datastart = NULL; indexsize = Col * sizeof (void *); // memory space to be applied: col pointer memory space array = (void **) malloc (indexsize + size * row * col ); // The size of the actually applied memory space includes the space of the matrix element + the extra requested pointer space datastart = (char *) array + indexsize; For (j = 0; j <row; j ++) {array [J] = datastart + size * Col * j;} return array;} int main () {int max_sub_array [2] [2] = {0}; // The Matrix of two rows and two columns stores the final result: two-dimensional matrix int original_array [3] [5] = {, 4}, {, 1, 0 }}; // target: the two-dimensional submatrix display_array (INT **) original_array, 3, 5) with the maximum element and of this matrix ); // call the function output original matrix max_2order_sqare (INT **) original_array, max_sub_array); // call the function to handle this problem, save the result in the matrix max_sub_array display_array (INT **) max_sub_array, 2, 2); // output the final search result return 0 ;} /* to dynamically apply a two-dimensional array, You need to carefully study the value transfer problem of a two-dimensional array with uncertain dimensions */

Iv. Summary

I thought this problem was very simple, because the algorithm was very simple, but it was overwhelmed by two-dimensional arrays and pointers when writing code. If it is not applicable to dynamically creating two-dimensional arrays, it is easier to use a fixed-dimensional two-dimensional array. I also know the difficulties of the C language, but I really did not expect the two-dimensional array and pointer together, it really can not afford to hurt.

There are two main difficulties encountered: one is that when writing the child function of the elements of the two-dimensional array, it is found that the size of the Second-dimensional array is always fixed when the two-dimensional array is passed, the significance of this subfunction is too small. Therefore, we hope to pass in an array pointer and then pass in rows and columns. This function is very useful. I found an article on the Internet and used a two-dimensional pointer to solve this problem. I am not very familiar with it. Let's use it first, and I will go into it later. Another problem is the dynamic creation of two-dimensional arrays. It is also a method found on the Internet. The principle is really not understandable. Please study it later.

 

 

 

 

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.