#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
- Include the Standard Library:
- The line
#include <stdio.h>
allows the program to use standard input and output functions, likeprintf
andscanf
.
- The line
- Variable Declarations:
int n, i;
: Here,n
will hold the number of elements the user wants in the array, whilei
is used as a loop counter.int max, min;
: These variables will store the maximum and minimum values found in the array.
- User Input for Array Size:
- The program prompts the user to enter the number of elements in the array:cCopy code
printf("Enter the number of elements in the array: ");
- The input is read using
scanf("%d", &n);
.
- The program prompts the user to enter the number of elements in the array:cCopy code
- Array Declaration:
int array[n];
: This declares an array of integers, where the size is determined by user input.
- 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
ton-1
, reading and storing each element in the array.
- A
- Initialize Max and Min:
- Both
max
andmin
are initialized to the first element of the array:cCopy codemax = min = array[0];
- This sets a starting point for comparisons.
- Both
- 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 currentmin
and updates them accordingly.
- A second
- Display the Results:
- The program prints the maximum and minimum values found in the array:cCopy code
printf("The maximum value in the array is: %d\n", max); printf("The minimum value in the array is: %d\n", min);
- The program prints the maximum and minimum values found in the array:cCopy code
- Program End:
- Finally, the program returns
0
, indicating it finished successfully.
- Finally, the program returns
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