Maximum Value in an Array using Loops in C
To find the maximum value in an array using loops in C, we iterate through the array, compare each element with a variable storing the maximum value, and update it whenever we find a larger number. This ensures that by the end of the loop, we have the maximum element in the array.
Examples of Finding the Maximum Value in an Array
1. Find Maximum Value using a for Loop
In this example, we will iterate through the array using a for loop, keep track of the largest number found, and display the maximum value at the end.
main.c
#include <stdio.h>
int main() {
int numbers[] = {5, 12, 8, 21, 7, 18};
int size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0]; // Assume the first element is the maximum
// Iterate through the array to find the maximum value
for (int i = 1; i < size; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
printf("Maximum value: %d\n", max);
return 0;
}
Explanation:
- We declare an integer array
numbers[]with six elements. - The variable
sizestores the total number of elements in the array. - We initialize
maxwith the first element of the array, assuming it is the largest initially. - The
forloop starts from index1and iterates through the array. - Inside the loop, we compare each element with
max. If the current element is greater, we updatemax. - After the loop completes,
maxholds the highest value in the array, which we print usingprintf().
Output:
Maximum value: 21
2. Find Maximum Value using a while Loop
In this example, we will use a while loop instead of a for loop to iterate through the array and find the maximum value.
main.c
#include <stdio.h>
int main() {
int numbers[] = {15, 22, 9, 30, 11, 25};
int size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0]; // Assume the first element is the maximum
int i = 1;
// Using while loop to find the maximum value
while (i < size) {
if (numbers[i] > max) {
max = numbers[i];
}
i++;
}
printf("Maximum value: %d\n", max);
return 0;
}
Explanation:
- We declare an integer array
numbers[]with six elements. - We calculate
sizeto determine the number of elements. maxis initialized with the first element.- We initialize
ito1before starting thewhileloop. - The loop runs while
iis less thansize. - Inside the loop, we compare
numbers[i]withmax. If it’s larger, we updatemax. iis incremented in each iteration until all elements are checked.
Output:
Maximum value: 30
3. Find Maximum Value using a do-while Loop
In this example, we use a do-while loop, ensuring the loop executes at least once, to find the maximum value in the array.
main.c
#include <stdio.h>
int main() {
int numbers[] = {3, 18, 6, 14, 27, 20};
int size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0]; // Assume the first element is the maximum
int i = 1;
// Using do-while loop to find the maximum value
do {
if (numbers[i] > max) {
max = numbers[i];
}
i++;
} while (i < size);
printf("Maximum value: %d\n", max);
return 0;
}
Explanation:
- We declare an integer array
numbers[]with six elements. - The variable
sizedetermines the number of elements. maxis initialized with the first element.- The
do-whileloop executes at least once. - Inside the loop, we check if
numbers[i]is greater thanmaxand update it if true. - The loop continues while
iis less thansize.
Output:
Maximum value: 27
Conclusion
In this tutorial, we covered three different ways to find the maximum value in an array using loops in C: for, while, and do-while. Each approach iterates through the array, compares values, and updates the maximum accordingly.
