๐ C++ Programming โ Part 16: Multi-Dimensional Arrays ๐ข So far, weโve used one-dimensional arrays (lists). Now, letโs level up with multi-dimensional arrays โ like tables or grids ๐ก --- ๐น What Are They? A 2D array is basically an array of arrays. Think of it as rows and columns ๐ int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} }; This creates 2 rows ร 3 columns = 6 elements. --- ๐น Accessing Elements: cout << matrix[0][2]; // 3 cout << matrix[1][1]; // 5 The first index = row, the second = column. --- ๐น Looping Through a 2D Array: for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << matrix[i][j] << " "; } cout << endl; } โ Output: 1 2 3 4 5 6 --- ๐น Why Use Them? 2D arrays are perfect for grids, game maps, matrices, or seating charts ๐ฎ --- ๐น Coming next (Part 17): Weโll explore vectors โ dynamic arrays that grow and shrink automatically ๐
- 1 reply
- 2 recasts
- 3 reactions
๐ C++ Programming โ Part 15: Arrays & Memory โก Now that you can loop through arrays, itโs time to understand how they live in memory โ and why C++ gives you so much control ๐ --- ๐น What Is an Array? An array is a group of variables of the same type stored next to each other in memory ๐ int nums[3] = {10, 20, 30}; Each element is stored in order โ and you can access them by index: cout << nums[0]; // 10 --- ๐น Arrays & Memory Addresses: Every element has its own memory address ๐ข
- 1 reply
- 1 recast
- 3 reactions
๐ C++ Programming โ Part 14: Loops with Strings & Arrays ๐ Now that you know how strings and arrays work โ letโs learn how to loop through them efficiently ๐ ๐น Looping Through an Array: Use a for loop to go through each element one by one ๐ int numbers[] = {10, 20, 30, 40}; for (int i = 0; i < 4; i++) { cout << numbers[i] << " "; } โ Output: 10 20 30 40 --- ๐น Looping Through a String: Each character in a string can be accessed like an array element ๐ string word = "Hello"; for (int i = 0; i < word.length(); i++) { cout << word[i] << " "; } โ Output: H e l l o --- ๐น Using Range-Based For Loop (Modern C++): A cleaner and more readable way ๐ for (char c : word) { cout << c << " "; } โ Same result โ but shorter and safer! --- ๐น Coming next (Part 15): Weโll explore arrays & memory together โ how theyโre stored and how pointers interact with them โก
- 1 reply
- 2 recasts
- 3 reactions