gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a C++ program. Initialize a 2d array (3x10) as follows:…
- User
Write a C++ program. Initialize a 2d array (3x10) 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 for that
- Assistant
Here's a C++ program that initializes a 2D array (3x10) and uses a function to accomplish that:
#include <iostream> // Function to initialize 2D array with given values void initializeArray(int arr[][10], int rows) { arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[0][3] = 4; arr[0][4] = 5; arr[0][5] = 1; arr[0][6] = 2; arr[0][7] = 3; arr[0][8] = 4; arr[0][9] = 5; arr[1][0] = 1; arr[1][1] = 0; arr[1][2] = 1; arr[1][3] = 0; arr[1][4] = 1; arr[1][5] = 0; arr[1][6] = 1; arr[1][7] = 0; arr[1][8] = 1; arr[1][9] = 0; arr[2][0] = 5; arr[2][1] = 4; arr[2][2] = 3; arr[2][3] = 2; arr[2][4] = 1; arr[2][5] = 5; arr[2][6] = 4; arr[2][7] = 3; arr[2][8] = 2; arr[2][9] = 1; } int main() { // Declare and initialize the 2D array int arr[3][10]; // Call the function to initialize the array initializeArray(arr, 3); // Print the initialized array for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } return 0; }The program declares a 2D array of size 3x10 and then calls the function to set the values of the array according to the given values. Finally, it prints the initialized array.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.