C Program to Find the Maximum and Minimum in an Array

Introduction

Finding the maximum and minimum values in an array is a common task in programming. This guide will show you how to write a C program to find the maximum and minimum elements in an array provided by the user.

Problem Statement

Create a C program that:

  • Takes the size of the array as input from the user.
  • Takes the elements of the array as input.
  • Finds the maximum and minimum elements in the array.
  • Displays the maximum and minimum elements.

Example:

  • Input: Array = [10, 20, 4, 45, 99]
  • Output: Maximum = 99, Minimum = 4

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the array size, the array elements, and variables to store the maximum and minimum values.
  4. Input the Array Size: Use scanf to take input from the user for the size of the array.
  5. Input the Elements of the Array: Use a loop to take input from the user for the elements of the array.
  6. Find the Maximum and Minimum Elements: Use a loop to iterate through the array and find the maximum and minimum elements.
  7. Display the Maximum and Minimum Elements: Use printf to display the maximum and minimum elements.

C Program

#include <stdio.h>

/**
 * C Program to Find the Maximum and Minimum in an Array
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the array size, elements, and max/min values
    int n, i, max, min;

    // Step 2: Prompt the user to enter the size of the array
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    // Step 3: Declare an array to hold the elements
    int arr[n];

    // Step 4: Input the array elements
    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Step 5: Initialize max and min with the first element of the array
    max = min = arr[0];

    // Step 6: Find the maximum and minimum elements
    for (i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    // Step 7: Display the maximum and minimum elements
    printf("Maximum element = %d\n", max);
    printf("Minimum element = %d\n", min);

    return 0;  // Step 8: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the size of the array. max and min are used to store the maximum and minimum elements in the array, respectively. The variable i is used as a loop counter.

Step 2: Input the Array Size

  • The program prompts the user to enter the size of the array using printf. The scanf function reads the input and stores it in the variable n.

Step 3: Declare the Array

  • The program declares an array arr of size n to hold the elements provided by the user.

Step 4: Input the Array Elements

  • The program uses a for loop to take input for each element of the array. The loop iterates from 0 to n-1, reading the elements using scanf.

Step 5: Initialize max and min

  • The program initializes max and min to the first element of the array (arr[0]). This provides a starting point for comparing other elements in the array.

Step 6: Find the Maximum and Minimum Elements

  • The program uses a for loop to iterate through the array starting from the second element (index 1):
    • If an element is greater than max, max is updated to that element.
    • If an element is smaller than min, min is updated to that element.

Step 7: Display the Maximum and Minimum Elements

  • The program displays the maximum and minimum elements found in the array using the printf function.

Step 8: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of elements in the array: 5
Enter 5 elements:
10
20
4
45
99
Maximum element = 99
Minimum element = 4

Another Example:

Enter the number of elements in the array: 6
Enter 6 elements:
-10
-20
0
50
100
-5
Maximum element = 100
Minimum element = -20

Conclusion

This C program demonstrates how to find the maximum and minimum elements in an array. It covers basic concepts such as arrays, loops, and conditional statements, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top