#include <stdio.h>

int main() {
    int n, i; // Declare variables for the number of elements and a loop counter
    int max, min; // Variables to store the maximum and minimum values

    // Ask the user for the number of elements in the array
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    int array[n]; // Declare an array of size n

    // Prompt the user to enter the array elements
    printf("Enter the elements of the array:\n");
    for (i = 0; i < n; i++) {
        printf("Element %d: ", i + 1);
        scanf("%d", &array[i]); // Read each element into the array
    }

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

    // Iterate through the array to find max and min
    for (i = 1; i < n; i++) {
        if (array[i] > max) {
            max = array[i]; // Update max if the current element is greater
        }
        if (array[i] < min) {
            min = array[i]; // Update min if the current element is smaller
        }
    }

    // Display the results
    printf("The maximum value in the array is: %d\n", max);
    printf("The minimum value in the array is: %d\n", min);

    return 0; // End of the program
}

Explanation of the Program

  1. Include the Standard Library:
    • The line #include <stdio.h> allows the program to use standard input and output functions, like printf and scanf.
  2. Variable Declarations:
    • int n, i;: Here, n will hold the number of elements the user wants in the array, while i is used as a loop counter.
    • int max, min;: These variables will store the maximum and minimum values found in the array.
  3. User Input for Array Size:
    • The program prompts the user to enter the number of elements in the array:cCopy codeprintf("Enter the number of elements in the array: ");
    • The input is read using scanf("%d", &n);.
  4. Array Declaration:
    • int array[n];: This declares an array of integers, where the size is determined by user input.
  5. Input Elements into the Array:
    • A for loop prompts the user to enter each element:cCopy codefor (i = 0; i < n; i++) { printf("Element %d: ", i + 1); scanf("%d", &array[i]); }
    • This loop runs from 0 to n-1, reading and storing each element in the array.
  6. Initialize Max and Min:
    • Both max and min are initialized to the first element of the array:cCopy codemax = min = array[0];
    • This sets a starting point for comparisons.
  7. Finding Maximum and Minimum Values:
    • A second for loop goes through the array starting from the second element:cCopy codefor (i = 1; i < n; i++) { if (array[i] > max) { max = array[i]; // Update max if the current element is larger } if (array[i] < min) { min = array[i]; // Update min if the current element is smaller } }
    • For each element, the program checks if it’s greater than the current max or less than the current min and updates them accordingly.
  8. Display the Results:
    • The program prints the maximum and minimum values found in the array:cCopy codeprintf("The maximum value in the array is: %d\n", max); printf("The minimum value in the array is: %d\n", min);
  9. Program End:
    • Finally, the program returns 0, indicating it finished successfully.

Example Output

Here’s how the program would interact with a user:

User Input:

Enter the number of elements in the array: 5
Enter the elements of the array:
Element 1: 34
Element 2: 12
Element 3: 45
Element 4: 7
Element 5: 28

Program Output:

maximum value in the array is: 45
The minimum value in the array is: 7

Leave a Reply

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

Verified by MonsterInsights