Write a C++ program. Initialize a 2d array (3x10) when declaring as follows:…
- User
Write a C++ program. Initialize a 2d array (3x10) when declaring as follows: {{1,2,3,4,5,1,2,3,4,5},{1,0,1,0,1,0,1,0,1,0},{5,4,3,2,1,5,4,3,2,1}}. Use a function to print it. Rows and columns are const values
- Assistant
Here is a C++ program that initializes a 2D array and uses a function to print it. The rows and columns are declared as constant values.
cpp#include <iostream> const int ROWS = 3; const int COLS = 10; void printArray(const int arr[ROWS][COLS]) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } } int main() { int arr[ROWS][COLS] = {{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}, {1, 0, 1, 0, 1, 0, 1, 0, 1, 0}, {5, 4, 3, 2, 1, 5, 4, 3, 2, 1}}; // Print the array printArray(arr); return 0; }This program declares
ROWSandCOLSas constant values. It then initializes a 2D arrayarrwith the provided values. The function takes this array as an argument and prints each element in a row-column format using nested loops. Finally, the function calls the function to print the array. - User
Now pass the array using reinterpret_cast operator
- Assistant
To pass the array using reinterpret_cast operator, you can modify the printArray function as follows:
cppvoid printArray(const int* arr) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { std::cout << *(arr + i * COLS + j) << " "; } std::cout << std::endl; } }In this updated version, the printArray function takes a pointer to int as an argument. Inside the function, the elements of the array are accessed using pointer arithmetic. The expression
*(arr + i * COLS + j)is used to access the appropriate element of the 2D array.To pass the array using reinterpret_cast in the main function, you can use the following code:
cpp - User
now pass rows and cols as seperate parameters
- Assistant
To pass the rows and cols as separate parameters to the printArray function, you can modify the function signature and code as follows:
cppvoid printArray(const int* arr, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { std::cout << *(arr + i * cols + j) << " "; } std::cout << std::endl; } }In this updated version of the printArray function, the dimensions of the array (rows and cols) are passed as separate parameters. Inside the function, these values are used to iterate through the array correctly.
Next, in the main function, you need to make the necessary changes to pass the rows and cols values to the printArray function:
cpp - User
Change the functions body so that it uses a[i][j] formula instead
- Assistant
Sure! Here's the modified printArray function that uses the
a[i][j]formula instead of pointer arithmetic:cppvoid printArray(const int* arr, int rows, int cols) { const int (*a)[cols] = reinterpret_cast<const int (*)[cols]>(arr); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { std::cout << a[i][j] << " "; } std::cout << std::endl; } }In this updated version, the
arrpointer is first converted to a pointer to a 2D array usingreinterpret_cast. Theconst int (*a)[cols]declaration represents a pointer to a 2D array of constant integers withcolscolumns. This allows us to access the elements of the original 2D array using thea[i][j]formula.The rest of the code remains as before. In the main function, the
ROWSandCOLSvalues are passed along with thearrpointer to the modified printArray function. - User
now combine it with the main function
- Assistant
Certainly! Here's the updated code combining both the modified printArray function and the main function:
cpp#include <iostream> const int ROWS = 3; const int COLS = 10; void printArray(const int* arr, int rows, int cols) { const int (*a)[cols] = reinterpret_cast<const int (*)[cols]>(arr); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { std::cout << a[i][j] << " "; } std::cout << std::endl; } } int main() { int arr[ROWS][COLS] = {{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}, {1, 0, 1, 0, 1, 0, 1, 0, 1, 0}, {5, 4, 3, 2, 1, 5, 4, 3, 2, 1}}; // Print the array printArray(reinterpret_cast<int*>(arr), ROWS, COLS); return 0; }