← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 5 of 15

C++ के Multi-dimensional Arrays

एक multi-dimensional array rows और columns वाली values की एक table है, एक chessboard या spreadsheet की तरह। आप एक item दो numbers से ढूंढते हैं, row और column।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. row और column indexes swap करना, जैसे a[col][row]।
  2. एक 2D array को function में pass करते समय column size छोड़ना, जो ज़रूरी है।
  3. यह मान लेना कि एक partially initialized 2D array random है, जब missing elements 0 बन जाते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.