Write a C++ program. Initialize and print a two-dimensional array using functions. Pass…
- User
Write a C++ program. Initialize and print a two-dimensional array using functions. Pass the array using reference
- Assistant
Here is an example of a C++ program that initializes and prints a two-dimensional array using functions and passing the array by reference:
#include <iostream> void initializeArray(int (&arr)[3][3]) { int count = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { arr[i][j] = count++; } } } void printArray(const int (&arr)[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } } int main() { int array[3][3]; initializeArray(array); printArray(array); return 0; }In this program, the
initializeArrayfunction takes a two-dimensional arrayarras a reference parameter and initializes it with values from 1 to 9. TheprintArrayfunction also takes the two-dimensional arrayarras a reference parameter and prints its values.In the main function, we declare a two-dimensional array
arrayand pass it to theinitializeArrayfunction to initialize its values. Then, we pass the same array to theprintArrayfunction to print its values. - User
Now make rows and columns a const values
- Assistant
Sure! Here's an updated version of the program where the number of rows and columns are constant values:
#include <iostream> const int ROWS = 3; const int COLS = 3; void initializeArray(int (&arr)[ROWS][COLS]) { int count = 1; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { arr[i][j] = count++; } } } 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 array[ROWS][COLS]; initializeArray(array); printArray(array); return 0; }In this updated version, the
ROWSandCOLSvariables are declared as constant values using theconstkeyword. These values are used to define the size of the two-dimensional arrayarrayand are also passed as arguments to theinitializeArrayandprintArrayfunctions.Now, if you want to change the number of rows or columns, you only need to modify the values of the
ROWSandCOLSvariables in the program. - User
Now pass the array using pointer
- Assistant
Here's an example program where the two-dimensional array is passed using a pointer instead of a reference:
#include <iostream> const int ROWS = 3; const int COLS = 3; void initializeArray(int *arr, int rows, int cols) { int count = 1; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { *(arr + i * cols + j) = count++; } } } void 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; } } int main() { int array[ROWS][COLS]; initializeArray((int *)array, ROWS, COLS); printArray((int *)array, ROWS, COLS); return 0; }In this program, the
initializeArrayandprintArrayfunctions take a pointer to an integerarr, and additional parametersrowsandcolsto indicate the dimensions of the array.To access the elements of the two-dimensional array, we use pointer arithmetic by iterating over
iandjand calculating the memory address of each element using the formulaarr + i * cols + j. The*(arr + i * cols + j)notation is used to dereference the memory location and access the value stored at that address.In the
mainfunction, we pass thearraypointer by casting it to an(int *)type when calling theinitializeArrayandprintArrayfunctions. This allows us to pass the array and its dimensions to the functions using pointers.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.