C Program to Find the Largest Element in an Array
C Program to Find the Largest Element in an Array
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The largest array element task needs a clear rule about order, duplicates and how many elements exist.
Find a maximum by keeping the largest element seen so far, initialized from a real element.
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 largest array element uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Find the smallest element instead, without sorting the data.
Largest Array Element: follow the data
Given data
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The largest array element task needs a clear rule about order, duplicates and how many elements exist.
Rule
Find a maximum by keeping the largest element seen so far, initialized from a real element.
Observed result
Largest: 50
Understand the operation
After each comparison, largest summarizes every visited element. Starting from marks[0] keeps the algorithm valid for a nonempty array containing only negative values.
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 largest = marks[0];
for (int i = 1; i < count; i++) if (marks[i] > largest) largest = marks[i];
printf("Largest: %d\n", largest);
return 0;
}
Expected output
Largest: 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 largest = marks[0];
for (int i = 1; i < count; i++) if (marks[i] > largest) largest = marks[i];
printf("Largest: %d\n", largest);
return 0;
}
The running maximum is 40, stays 40 for 35, becomes 45 and eventually becomes 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 running maximum is 40, stays 40 for 35, becomes 45 and eventually becomes 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
Largest: 50
Largest: 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 largest = marks[0];
for (int i = 1; i < count; i++) if (marks[i] > largest) largest = marks[i];
printf("Largest: %d\n", largest);
return 0;
}
Find the smallest element instead, without sorting the data.
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 smallest = marks[0];
for (int i = 1; i < count; i++) if (marks[i] < smallest) smallest = marks[i];
printf("Smallest: %d\n", smallest);
return 0;
}
Expected output
Smallest: 35
Run in C compiler →Recognize the important boundaries
An empty array has no largest element to initialize from. Decide how the caller reports absence before reading marks[0].
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
Find a maximum by keeping the largest element seen so far, initialized from a real element.
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.