C language basic algorithm 37-maximum array value and its location, algorithm 37 Maximum array value
// Array Operation
/*
========================================================== ======================================
Question: Find the maximum element of the array and output its position and value!
========================================================== ======================================
*/
# Include <stdio. h>
# Define M 3
# Define N 5
Void main ()
{
Int a [M] [N], I, j, max, h, l;
Printf ("input % d * % d matrix: \ n", M, N );
For (I = 0; I <M; I ++)
For (j = 0; j <N; j ++)
Scanf ("% d", & a [I] [j]);
Max = a [0] [0];
Printf ("matrix output: \ n ");
For (I = 0; I <M; I ++)
{
For (j = 0; j <N; j ++)
Printf ("% 4d", a [I] [j]);
Printf ("\ n ");
}
For (I = 0; I <M; I ++)
For (j = 0; j <N; j ++)
If (max <a [I] [j])
{
Max = a [I] [j];
H = I;
L = j;
}
Printf ("the maximum element is: a [% d] [% d], that is, row % d. The value of column % d \ n is % d \ n ", h + 1, l + 1, h + 1, l + 1, max );
}
/*
========================================================== ====================================
Rating:
For a simple question, you only need to find out the method clearly, so that max is any element of the matrix and compares it with all elements,
If max is smaller than it, let max = it, and record the rows and columns at this time until the search ends! To comply
People's normal thinking, the rows and columns should be added with 1, because the computer starts from 0 by default, and people generally
Starting from 1!
Thinking: the size of the matrix in this program can be determined by yourself, but it is not flexible enough. Try to use the input number.
To determine the array size, making the program more flexible.
========================================================== ======================================
*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.