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

</>
Copy
#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:

  1. We declare an integer array numbers[] with six elements.
  2. The variable size stores the total number of elements in the array.
  3. We initialize max with the first element of the array, assuming it is the largest initially.
  4. The for loop starts from index 1 and iterates through the array.
  5. Inside the loop, we compare each element with max. If the current element is greater, we update max.
  6. After the loop completes, max holds the highest value in the array, which we print using printf().

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

</>
Copy
#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:

  1. We declare an integer array numbers[] with six elements.
  2. We calculate size to determine the number of elements.
  3. max is initialized with the first element.
  4. We initialize i to 1 before starting the while loop.
  5. The loop runs while i is less than size.
  6. Inside the loop, we compare numbers[i] with max. If it’s larger, we update max.
  7. i is 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

</>
Copy
#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:

  1. We declare an integer array numbers[] with six elements.
  2. The variable size determines the number of elements.
  3. max is initialized with the first element.
  4. The do-while loop executes at least once.
  5. Inside the loop, we check if numbers[i] is greater than max and update it if true.
  6. The loop continues while i is less than size.

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.