Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

It is required to develop in C++ language, without using third-party libraries,…

  1. User

    It is required to develop in C++ language, without using third-party libraries, frameworks, and the standard template library the following program: A program for creating a two-dimensional dynamic array from one-dimensional. The user should be able to set the size one-dimensional array, the width of a two-dimensional one, delete old data and create new ones. The array is filled with random data in the range, specified by the user. It must be possible to withdraw both arrays as a whole or an arbitrary element of each. Program supports working with arrays with at least 1^8 elements.

  2. Assistant

    Here is an example implementation of the required program using C++:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    // Function to create a two-dimensional dynamic array from a one-dimensional array
    int** create2DArray(int* arr, int size, int width) {
        int** arr2D = new int*[size / width];
        for (int i = 0; i < size / width; i++) {
            arr2D[i] = new int[width];
            for (int j = 0; j < width; j++) {
                arr2D[i][j] = arr[i * width + j];
            }
        }
        return arr2D;
    }
    
    // Function to delete a two-dimensional dynamic array
    void delete2DArray(int** arr2D, int size) {
        for (int i = 0; i < size; i++) {
            delete[] arr2D[i];
        }
        delete[] arr2D;
    }
    
    // Function to fill the array with random data within the specified range
    void fillArray(int* arr, int size, int rangeMin, int rangeMax) {
        for (int i = 0; i < size; i++) {
            arr[i] = rand() % (rangeMax - rangeMin + 1) + rangeMin;
        }
    }
    
    // Function to print the one-dimensional array
    void print1DArray(int* arr, int size) {
        std::cout << "[ ";
        for (int i = 0; i < size; i++) {
            std::cout << arr[i] << " ";
        }
        std::cout << "]" << std::endl;
    }
    
    // Function to print the two-dimensional array
    void print2DArray(int** arr2D, int size, int width) {
        std::cout << "[ " << std::endl;
        for (int i = 0; i < size; i++) {
            std::cout << "  [ ";
            for (int j = 0; j < width; j++) {
                std::cout << arr2D[i][j] << " ";
            }
            std::cout << "]" << std::endl;
        }
        std::cout << "]" << std::endl;
    }
    
    int main() {
        srand(time(0));  // Set seed for random number generation
    
        int* arr = nullptr;
        int size;
        int width;
    
        // Read size of the one-dimensional array from user
        std::cout << "Enter the size of the one-dimensional array: ";
        std::cin >> size;
    
        // Allocate memory for the one-dimensional array
        arr = new int[size];
    
        // Read width of the two-dimensional array from user
        std::cout << "Enter the width of the two-dimensional array: ";
        std::cin >> width;
    
        // Fill the array with random data in the specified range
        int rangeMin, rangeMax;
        std::cout << "Enter the minimum value for the random range: ";
        std::cin >> rangeMin;
        std::cout << "Enter the maximum value for the random range: ";
        std::cin >> rangeMax;
        fillArray(arr, size, rangeMin, rangeMax);
    
        // Create the two-dimensional array from the one-dimensional array
        int** arr2D = create2DArray(arr, size, width);
    
        // Print the one-dimensional array
        std::cout << "One-dimensional array: ";
        print1DArray(arr, size);
    
        // Print the two-dimensional array
        std::cout << "Two-dimensional array: " << std::endl;
        print2DArray(arr2D, size / width, width);
    
        // Clean up memory
        delete[] arr;
        delete2DArray(arr2D, size / width);
    
        return 0;
    }
    

    In this program, we start by declaring and implementing several helper functions:

    1. create2DArray: This function takes a one-dimensional array (arr), its size (size), and the desired width of the two-dimensional array (width). It creates a new two-dimensional array dynamically, copies the elements from the one-dimensional array to the two-dimensional array based on the specified width, and returns the pointer to the two-dimensional array.

    2. delete2DArray: This function takes a two-dimensional array (arr2D) and its size (size). It deletes the dynamically allocated memory for the two-dimensional array.

    3. fillArray: This function takes a one-dimensional array (arr), its size (size), and the range for random data generation (rangeMin and rangeMax). It fills the array with random data within the specified range.

    4. print1DArray: This function takes a one-dimensional array (arr) and its size (size). It prints the array elements in a formatted manner.

    5. print2DArray: This function takes a two-dimensional array (arr2D), its size (size), and the width of the two-dimensional array (width). It prints the array elements in a formatted manner.

    In the main function, we start by reading the size of the one-dimensional array and the width of the two-dimensional array from the user. Then, we read the range for random data generation. We allocate memory for the one-dimensional array and fill it with random data. Later, we create the two-dimensional array from the one-dimensional array using the create2DArray function and print both arrays. Finally, we clean up the allocated memory using delete and delete2DArray functions.