You're online now.

Hurray! you are online now.

Write a C program to find the minimum element in the array

The C program finds the minimum element in an array entered by the user..

//Write a  cprogram to find the minimum element in array
    #include<stdio.h>
    // find min value function
    int minimumInArray(int arr[], int n){
    	int  min = arr[0];
    	for(int i = 0; i < n; i++){
    		if(arr[i] < min){
    			min = arr[i];
    		}
    	}
    	return min;
    }
    int main(){
        int n;
        printf("Enter the size of the array : ");
        scanf("%d", &n);
        int arr[n];
        // input array from the user
        for(int i = 0; i < n; i++){
            scanf("%d", &arr[i]);
        }
        printf("%d is the minimum number", minimumInArray(arr, n));
        return 0;
    }
#include<stdio.h>

#include<stdio.h> is a preprocessor directive that includes the standard library input/output function. Which is used to use the printf() and scanf() function.

// function to find the minimum
    int minimumInArray(int arr[], int n){
        int min = arr[0];
        for(int i = 0; i < n; i++){
            if(arr[i] < min){
                min = arr[i];
            }
        }
        return min;
    }

The minimumInArray() function finds the minimum value in the given array from the user, parameter ‘arr’ takes the all elements which entered from the user, parameter ‘n’ is contains the size of the array. In the last returned the minimum value in the array. ‘int min = arr[0]’ initializing the min element with the frist element of the array, which is help to compare the another value of the element using follow the for loop. Inside the for loop we check every element in the array if any element is minimum of the min value then we change the min value that a particular value, afte return the min value.

int main(){
        int n;
        printf("Enter the size of the array : ");
        scanf("%d", &n);
        int arr[n];
        // input array from the user
        for(int i = 0; i < n; i++){
            scanf("%d", &arr[i]);
        }
        printf("%d is the minimum number", minimumInArray(arr, n));
        return 0;
    }

The above main() function is the entry point of the program. ‘int n’ is store the size of the array which takes the input from the user. ‘int ar[n]’ is declared the array ‘n’ size which given by the user. Takes the input in array using the for loop and store the element in array at a particular indexed. After takes the array input from the user print the minimum value in the array. return 0; means that the program is successfully executed.

Algorithm

1. Start
2. Read the size of the array from the USER
3. Set the array of the given size
4. Read the element of the array from the USER
4. Find the minimum value in the array
5. Print the minimum number in the array
6. End

Pseudocde

1. INCLUDE stdio.h
2. FUNCTION minimumInArray(int arr[], int n)
    1. SET min = arr[0]
    2. FOR i = 0 to i < n
        1. IF arr[i] < min
            min = arr[i]
    3. RETURN min
3. FUNCTION main()
    1. SET n
    2. PRINT "Enter the size of the array"
    3. READ n FROM USER
    4. SET arr[n]
    5. READ the element of the array FROM USER
    6. PRINT minimum of the array
    7. RETURN 0
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...