शुरुआती C/C++ अभ्यास

Find the Largest Array Value

यह शुरुआती exercise Find the Largest Array Value के जरिए core syntax और problem solving का अभ्यास कराती है।

Find the Largest Array Value क्या है?

यह शुरुआती exercise Find the Largest Array Value के जरिए core syntax और problem solving का अभ्यास कराती है।

Find the Largest Array Value को C और C++ दोनों में runnable code से हल करें।

महत्वपूर्ण बातें

  • Compiler warnings चालू करके सभी ठीक करें।
  • हर value का type और lifetime समझें।
  • Input और array bounds validate करें।

C और C++ code examples

C
कोड चलाएँ →
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);
}
अपेक्षित output
max=23
C++
कोड चलाएँ →
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';
}
अपेक्षित output
max=23

C और C++ की तुलना

C और C++ implementations की तुलना करें, input बदलें और अतिरिक्त edge cases test करें।

C

C छोटे procedural APIs और representation details स्पष्ट करता है।

C++

C++ low-level model रखकर safer library types जोड़ता है।

अभ्यास

दोनों versions चलाकर बदलें और language guarantees की तुलना करें।

  • Invalid और boundary inputs जोड़ें।
  • -Wall -Wextra -Wpedantic से compile करें।
  • Declarations और implementation अलग करें।