Beginner C/C++ Exercises

Find the Largest Array Value

A running maximum tracks the best value seen so far.

What is Find the Largest Array Value?

A running maximum tracks the best value seen so far.

Find the maximum value in an array.

Important points

  • Compile with warnings enabled and fix every warning.
  • Know the lifetime and type of every value.
  • Validate input and keep array bounds explicit.

C and C++ code examples

C
Run code →
main.c
#include <stdio.h>
int main(void) {
    int values[] = {-4, 7, 23, 9, 12}; int maximum = values[0];
    // Compare every remaining value with the current maximum.
    for (size_t i = 1; i < sizeof values / sizeof values[0]; ++i)
        if (values[i] > maximum) maximum = values[i];
    printf("max=%d\n", maximum);
}
Expected output
max=23
C++
Run code →
main.cpp
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector values{-4, 7, 23, 9, 12};
    // max_element returns an iterator to the largest value.
    std::cout << "max=" << *std::max_element(values.begin(), values.end()) << '\n';
}
Expected output
max=23

C and C++ comparison

Initialize from the first element so negative-only arrays are handled correctly.

C

C exposes small procedural APIs and makes representation details explicit.

C++

C++ retains the low-level model while adding safer library types and abstractions.

Practice exercises

Run both versions, then modify them to observe the different language guarantees.

  • Add invalid and boundary-value inputs.
  • Compile with -Wall -Wextra -Wpedantic.
  • Move reusable declarations into a header and implementation file.