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

Transpose a Matrix

यह शुरुआती exercise Transpose a Matrix के जरिए core syntax और problem solving का अभ्यास कराती है।

Transpose a Matrix क्या है?

यह शुरुआती exercise Transpose a Matrix के जरिए core syntax और problem solving का अभ्यास कराती है।

Transpose a Matrix को 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 matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    // Visit columns first to print the transposed shape.
    for (int column = 0; column < 3; ++column) { for (int row = 0; row < 2; ++row) printf("%s%d", row ? " " : "", matrix[row][column]); puts(""); }
}
अपेक्षित output
1 4
2 5
3 6
C++
कोड चलाएँ →
main.cpp
#include <array>
#include <iostream>
int main() {
    std::array<std::array<int, 3>, 2> matrix{{{{1, 2, 3}}, {{4, 5, 6}}}};
    // Visit columns first to print the transposed shape.
    for (size_t column = 0; column < 3; ++column) { for (size_t row = 0; row < 2; ++row) std::cout << (row ? " " : "") << matrix[row][column]; std::cout << '\n'; }
}
अपेक्षित output
1 4
2 5
3 6

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 अलग करें।