int numbers[5] = {10, 20, 30, 40, 50}; // Declares and initializes an arrayint max = numbers[0];
for (int i = 1; i < 5; ++i) {
if (numbers[i] > max) {
max = numbers[i];
}
}
std::cout << "Maximum number: " << max << std::endl;int searchValue = 30;
bool found = false;
for (int i = 0; i < 5; ++i) {
if (numbers[i] == searchValue) {
found = true;
break;
}
}
if (found) {
std::cout << searchValue << " found in the array." << std::endl;
} else {
std::cout << searchValue << " not found in the array." << std::endl;
}int matrix[2][3] = { {1, 2, 3}, {4, 5, 6}}; // Declares a 2x3 matrixfor (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}int rowSum = 0;
for (int j = 0; j < 3; ++j) {
rowSum += matrix[0][j]; // Calculate sum of the first row
}
std::cout << "Sum of the first row: " << rowSum << std::endl;int cube[2][2][2] = { { {1, 2}, {3, 4}}, { { 5, 6}, {7, 8}}};for (int i = 0; i < 2; ++i) { // Iterate through depth
for (int j = 0; j < 2; ++j) { // Iterate through rows
for (int k = 0; k < 2; ++k) { // Iterate through columns
std::cout << cube[i][j][k] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
int* dynamicArray = new int[size]; for (int i = 0; i < size; ++i) {
std::cin >> dynamicArray[i];
}
std::cout << "Array elements:" << std::endl;
for (int i = 0; i < size; ++i) {
std::cout << dynamicArray[i] << " ";
}
std::cout << std::endl;delete[] dynamicArray; In C++, 2D arrays are typically stored in row-major order in memory. This means that the elements of each row are stored consecutively, followed by the elements of the next row.
int matrix[2][3] = { {10, 20, 30}, {40, 50, 60}};[10] [20] [30] | [40] [50] [60]
0x1000 0x1004 0x1008 | 0x100C 0x1010 0x1014
int* arr[3]; // Array of 3 pointers to integersarr[0] = new int[4]; // Allocate memory for the first row
arr[1] = new int[5]; // Allocate memory for the second row
arr[2] = new int[3]; // Allocate memory for the third rowstd::cout << arr[0][2] << std::endl; // Access element at row 0, column 2for (int i = 0; i < 3; ++i) {
delete[] arr[i]; // Deallocate memory for each row
}int (*ptr)[3]; // Pointer to an array of 3 integersint matrix[2][3] = { {1, 2, 3}, {4, 5, 6}};
ptr = matrix; // Assign address of the matrix to the pointerstd::cout << (*ptr)[0][1] << std::endl; // Access element at row 0, column 1#include <iostream>
int main() {
int rows = 2, cols = 3;
// Array of pointers
int* arr[rows];
for (int i = 0; i < rows; ++i) {
arr[i] = new int[cols];
}
// Initialize the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
arr[i][j] = i * cols + j + 1;
}
}
// Print the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
// Deallocate memory
for (int i = 0; i < rows; ++i) {
delete[] arr[i];
}
return 0;
}https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays