C Program to Find the Maximum and Minimum in an Array
C Program to Find the Maximum and Minimum in an Array
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The array minimum and maximum task needs a clear rule about order, duplicates and how many elements exist.
Track two summaries in one pass: the smallest and largest element already examined.
Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.
Explain
Explain why array minimum and maximum uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use the discovered minimum and maximum to print the spread for these bounded marks.
Array Minimum and Maximum: follow the data
Given data
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The array minimum and maximum task needs a clear rule about order, duplicates and how many elements exist.
Rule
Track two summaries in one pass: the smallest and largest element already examined.
Observed result
Minimum: 35; maximum: 50
Understand the operation
The comparisons are independent. A value can update either bound; the original array order need not change to discover its extremes.
The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int low = marks[0], high = marks[0];
for (int i = 1; i < count; i++) {
if (marks[i] < low) low = marks[i];
if (marks[i] > high) high = marks[i];
}
printf("Minimum: %d; maximum: %d\n", low, high);
return 0;
}
Expected output
Minimum: 35; maximum: 50
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int low = marks[0], high = marks[0];
for (int i = 1; i < count; i++) {
if (marks[i] < low) low = marks[i];
if (marks[i] > high) high = marks[i];
}
printf("Minimum: %d; maximum: %d\n", low, high);
return 0;
}
The first 35 lowers the minimum to 35. The final 50 raises the maximum to 50.
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Follow the worked calculation
Start with the sample
The first 35 lowers the minimum to 35. The final 50 raises the maximum to 50.
Apply the rule
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Check the result
Minimum: 35; maximum: 50
Minimum: 35; maximum: 50
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int low = marks[0], high = marks[0];
for (int i = 1; i < count; i++) {
if (marks[i] < low) low = marks[i];
if (marks[i] > high) high = marks[i];
}
printf("Minimum: %d; maximum: %d\n", low, high);
return 0;
}
Use the discovered minimum and maximum to print the spread for these bounded marks.
Worked answer
The complete answer keeps the five valid input elements and applies the stated alternative. Read the output against the original array rather than assume that every loop should sort, remove or overwrite values.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int low = marks[0], high = marks[0];
for (int i = 1; i < count; i++) {
if (marks[i] < low) low = marks[i];
if (marks[i] > high) high = marks[i];
}
printf("Spread: %d\n", high - low);
return 0;
}
Expected output
Spread: 15
Run in C compiler →Recognize the important boundaries
The difference high-low can overflow for unrestricted signed values, even when both endpoints individually fit.
The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Track two summaries in one pass: the smallest and largest element already examined.
Continue with array indexes and bounds.
Continue with pointer and count contracts.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.