C++ के Multi-dimensional Arrays
In this page:
data_type array_name[rows][columns];
data_type array_name[rows][columns] = {{v1, v2}, {v3, v4}};
array_name[row][column];
Two-Dimensional Arrays
एक two-dimensional array grid-shaped data model करता है — जैसे एक spreadsheet, एक game board, या एक matrix — values को rows और columns में organize करके, और यह conceptually सिर्फ एक array है जहाँ हर element खुद एक और array है।
int grid[3][4] declare करना 3 rows बनाता है, हर एक में 4 columns।
उदाहरण: Two-Dimensional Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3];
grid[0][0] = 1;
// Print to the console with cout
std::cout << grid[0][0] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
2D Arrays Initialize करना
Nested curly braces आपको एक 2D array को row by row initialize करने देते हैं, जैसे int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};, जो visually actual grid structure को mirror करता है और एक नज़र में obvious बनाता है कि कौन सी values किस row से belong करती हैं।
उदाहरण: Initializing 2D Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Print to the console with cout
std::cout << grid[1][2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
2D Arrays में Elements Access करना
एक specific element तक पहुँचने के लिए अलग brackets में दो indices चाहिए, जैसे grid[row][col], दोनों dimensions एक regular one-dimensional array की तरह zero से count होते हुए।
row और column order मिला देना एक common bug है, खासकर जब एक grid square न हो।
उदाहरण: Accessing Elements in 2D Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Print to the console with cout
std::cout << grid[0][1] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
2D Arrays में Elements Modify करना
एक 2D array में एक cell update करना exactly एक 1D array जैसा ही काम करता है, बस एक की बजाय दो indices के साथ — grid[1][2] = 99; सिर्फ उस single coordinate को बदलता है, बाकी हर element को untouched छोड़ते हुए।
उदाहरण: Modifying Elements in 2D Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
grid[1][2] = 99;
// Print to the console with cout
std::cout << grid[1][2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
2D Arrays के जरिए Loop करना
एक 2D array के हर element visit करने के लिए nested loops चाहिए: outer loop एक बार में rows में आगे बढ़ता है, और हर row के लिए, inner loop उस row के हर column में आगे बढ़ता है।
यह nested structure सीधे 2D array के अपने nested layout को mirror करता है, यही वजह है कि यह इसे traverse करने का natural तरीका लगता है।
उदाहरण: Looping Through 2D Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Loop: repeat while the condition holds
for (int row = 0; row < 2; row++) {
// Loop: repeat while the condition holds
for (int col = 0; col < 3; col++) {
// Print to the console with cout
std::cout << grid[row][col] << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- row और column indexes swap करना, जैसे
a[col][row]। - एक 2D array को function में pass करते समय column size छोड़ना, जो ज़रूरी है।
- यह मान लेना कि एक partially initialized 2D array random है, जब missing elements 0 बन जाते हैं।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings