Find Max Element in Array in C Language

program-find-max-element-in-array-in-c

Max Element in Array in C Program Description:

Program to find the Max Element in Array in C programming language. We ask the user to enter the elements of the array and find the Maximum element.

Expected Input and Output:

Input:

Output:

Prerequisites:

We need to know the basics of the C Array, Functions, and Passing Arrays to Function.

Find Max Element in Array in C Program Explanation:

  1. Declare an integer array named numbers. The numbers array holds 5 elements ( size of the array) and also declares a variable i to traverse the array.
  2. Take the input from the user and update the numbers array. Use a For Loop to iterate over the array and take the input from the user using standard input and output functions ( printf and scanf) and update the array elements.
  3. Create a variable called max. Initialize the max with INT_MIN. The value of the INT_MIN is -2147483648. Learn more about the INT_MIN at the Size and Limits of datatypes article.
  4. To find the Maximum element(max) we need to traverse the array element by element and update the max variable if any element is larger than the max. which is numbers[i] > max
  5. Continue above loop till size-1. and once the loop is terminated the variable max contains the Max element in the array.
  6. Display the max on the console.

Find Max Element in Array in C Algorithm:

  1. Declare array int values[5]; and int i
  2. Prompt the user to input the values for the array
  3. FOR  i = 0 to  SIZE-1 ( Loop )
    • Read the value for   values[i]
  4. Initialize max variable i.e max = INT_MIN;
  5. FOR  i = 0 to  SIZE-1 ( Loop )
    • update max( max = data[i]) if the data[i] > max is true
  6. Print the max value on the console

Program to Find Max Element in Array in C:

Here is the C Code for the above algorithm

Program Output:

Now compile the program

$ gcc max-array.c

We used the GCC compiler to run the program. The above command generates the a.out file, which is an executable file. Run the executable file.

max-element-in-array-in-c-program-output

Try another example

Method 2: Maximum Element in an Array in C using Functions:

Let’s divide the above program into the functions and each and every function will perform a specific task like reading the values from the user and calculating the maximum value.

The following program uses two functions

  1. The read() function.
    • Prototype of the read() function – void read(int numbers[], int size);
    • The read function takes two arguments – numbers array and its size. The read() function prompts the user for array elements and updates the number array.
  2. The maximum() function.
    • Prototype of the maximum() function – int maximum(int numbers[], int size);
    • Similar to the read() function, The maximum function also takes two formal arguments one is the input array numbers[] and the sizeof the number’s array.
    • The maximum() function finds the maximum element in the array by comparing all elements.
    • Finally, It returns the maximum element in the array.

Program Output:

Here is the output of the program.

max-function-in-c-program

We are getting the expected results.

Related Array Programs:

C Programs Index:

C Tutorials Index:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Find Maximum Element in Array […]

Leave a Reply