Home > Common Problem > How to find the maximum value in an array in c language

How to find the maximum value in an array in c language

百草
Release: 2023-06-19 13:32:43
Original
22108 people have browsed it

C language to find the maximum value in an array: 1. Define an array that can be used to store numbers as "arr[]"; 2. Use a for loop to store the numbers in "arr[]" ;3. Define the first number in the array as the maximum value, add 1 to the subscript, and get the second number. By comparing it with the first number, store the larger value in max, and add the subscript every time it is compared. 1. Repeat the operation; 4. Output the max value.

How to find the maximum value in an array in c language

The operating system of this tutorial: windows10 system, c99 version, DELL G3 computer.

The main idea of ​​finding the maximum value in an array in C language is to compare two by two, store the large value in a variable, and then continue to compare with the next value through a loop, and this cycle repeats , and finally the maximum value can be found.

Take ten numbers as an example to find the maximum value:

First, you should define an array that can be used to store numbers, such as:

How to find the maximum value in an array in c language

Note that this array should be of type int, because next we have to perform the scanf input operation. What we have to input is ten numbers for comparison. If the char type is used incorrectly, it may lead to out-of-bounds occurrences when the program is running. Warning (this has been happening before and I don’t know how to solve it).

The next step is to perform the input operation. We can use a loop to store ten digital inputs into an array. There is no need to store the values ​​one by one. That would be too inefficient, like this:

How to find the maximum value in an array in c language

Then, you can define the first number in the array as the maximum value, then add 1 to the subscript to get the second number, and compare it with the first number , store the larger value in max. Here, you need to use a loop. Every time you compare, add 1 to the subscript, and repeat the operation, such as:

How to find the maximum value in an array in c language

Finally, just The value of max can be printed out.

The complete code for the maximum value of the array is:

How to find the maximum value in an array in c language

Here, we can continue to think about whether this code can be promoted, for example Not only can you enter 10 numbers, but you can also enter more numbers, or you can enter less than ten values, which makes it more flexible and feasible.

We can enter as many numbers as we want according to our own wishes. The specific code after promotion is shown in the picture below:

How to find the maximum value in an array in c language

The above is the detailed content of How to find the maximum value in an array in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template