Finding the maximum and minimum values in an array is a common task in C programming that helps you understand array traversal and basic comparisons. Here’s how you can implement a program to accomplish this.

Example: Find Maximum and Minimum in an Array

In this example, we will write a program that allows the user to input a specific number of integers into an array and then finds and displays the maximum and minimum values.

C Program to Find Maximum and Minimum in an Array

#include <stdio.h> int main() { int n, i; int arr[100]; // Declare an array of size 100 // Input the number of elements in the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Input elements into the array printf("Enter %d integers:\n", n); for (i = 0; i < n; i++) { printf("Element %d: ", i + 1); scanf("%d", &arr[i]); // Store each element in the array } // Initialize max and min with the first element of the array int max = arr[0]; int min = arr[0]; // Traverse the array to find max and min for (i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; // Update max if current element is greater } if (arr[i] < min) { min = arr[i]; // Update min if current element is smaller } } // Output the maximum and minimum values printf("The maximum value in the array is: %d\n", max); printf("The minimum value in the array is: %d\n", min); return 0; }

Explanation:

  1. Variable Declarations:

    • int n: This variable holds the number of elements the user wants to input.
    • int i: Loop counter for iterating through the array.
    • int arr[100]: Declares an integer array of size 100 to store the elements.
  2. Input for Array Size:

    • The program prompts the user to enter the number of elements they want to analyze and stores this value in n.
  3. Input for Array Elements:

    • A for loop is used to iterate from 0 to n-1, prompting the user to input each integer, which is stored in the array arr.
  4. Finding Maximum and Minimum Values:

    • The variables max and min are initialized to the first element of the array (arr[0]).
    • A for loop is then used to traverse the array starting from the second element (i = 1):
      • If the current element arr[i] is greater than max, it updates max.
      • If the current element arr[i] is less than min, it updates min.
  5. Output the Maximum and Minimum Values:

    • Finally, the program prints the maximum and minimum values found in the array.

Sample Output:

Example 1:

Enter the number of elements in the array: 5 Enter 5 integers: Element 1: 23 Element 2: 15 Element 3: 42 Element 4: 8 Element 5: 16 The maximum value in the array is: 42 The minimum value in the array is: 8

Example 2:

Enter the number of elements in the array: 4 Enter 4 integers: Element 1: 7 Element 2: 5 Element 3: 12 Element 4: 0 The maximum value in the array is: 12 The minimum value in the array is: 0

Key Points:

  • Array Traversal: The program demonstrates how to iterate through an array using a loop.
  • Comparison Logic: Simple if statements are used to compare values and update the maximum and minimum values accordingly.
  • Input/Output: Standard input/output functions are employed to interact with the user.

Variations:

You can modify the program to:

  • Handle larger arrays dynamically using dynamic memory allocation (with malloc).
  • Find the maximum and minimum in specific ranges of the array based on user input.
  • Implement functions to encapsulate the logic for finding the maximum and minimum values, enhancing code modularity.

This program serves as a foundational exercise for understanding arrays, loops, and conditional statements in C programming.