C language basic algorithm 37-maximum array value and its location, algorithm 37 Maximum array value

Source: Internet
Author: User

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.

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.