Find Minimum Element in Array in C Language

Spread the love
Find-Minimum-Element-in-Array-in-C-Language

Find Minimum element in Array in C Program Description:

Write a Program to Find Minimum element in Array in C programming language. The program should accept the user input, update the array, and calculate the minimum element in the array.

Example Input and Output:

Input:

Output:

Minimum element in array is : 2

Prerequisites:

It is recommended to know the basics of the Arrays in C and functions in c. We have already covered the array and functions in our earlier posts, Please check them.

Find Minimum element in Array in C Program Explanation:

  1. Start the program by declaring an integer array named data. The data array holds 5 elements ( size of the array) and also declare a variable i to traverse the array.
  2. Take the input from the user and update the 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)
  3. Create a variable called min. Initialize the min with INT_MAX. The value of the INT_MAX is 2147483647. Learn more about the INT_MAX at Size and Limits of datatypes
  4. To find the minimum element we need to traverse the array and update the min if any element is smaller than the min. data[i] < min
  5. Once the above loop is completed the variable min contain the minimum element in the array.
  6. Display the results on the console.

The minimum element in Array in C Program Algorithm:

  1. Declare array int data[5]; and int i
  2. Prompt the user to input the values for the array
  3. FOR  i&nbsp;=&nbsp;0 to  SIZE-1 ( Loop )
    • Read the value for  data[i]
  4. Initialize minmin = INT_MAX;
  5. FOR  i&nbsp;=&nbsp;0 to  SIZE-1 ( Loop )
    • update min( min = data[i]) if the data[i] < min is true
  6. Print the min value on the console

Program to Find Minimum element in Array in C:

Let’s convert the above algorithm to code.

Program Output:

Compile and Run the program using GCC (any compiler)

find-minimum-element-in-array-in-c-program-output

With negative numbers

Find Minimum element in Array in C using Functions:

In the above program, we have used only the main() function. So let’s divide the above program into the functions So that we can re-use the code.

We have defined two functions here

  • read() function – The read() function is used to read the elements of the array.
    • Prototype of read function
    • void read(int *data, int size);
  • minimum function – The minimum function finds the minimum element in the array and returns it.
    • Prototype of minimum function
    • int minimum(int data[], int size);

📢We can use the pointer or array as the formal argument of the functions.

Program Output:

Now, Let’s compile and run the program and observe the output

min-element-in-array-in-c

As we can see from the above output, We are getting the desired results.

  1. C Program to Perform Arithmetic Operations using functions
  2. Add Two Numbers using Functions in C
  3. C Program to calculate Fibonacci Series using function
  4. C Program to find Factorial of a Number using functions
  5. C Program to find Minimum and Maximum numbers using Functions
  6. Armstrong Number using Function in C language
  7. Prime number program using Function
  8. Program to Caclulate Area of Circle using Function
  9. Program to Check Even or Odd number using Function
  10. Binary to Decimal Conversion using function in C

Venkat

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. […] to find the Second Largest Element in Array in C programming language. We have looked at the Minimum number in the array and Maximum Element in Array programs in earlier articles, In today’s article, We will look at […]

Leave a Reply