首页 > 解决方案 > 在二维数组中添加行

问题描述

用户输入期望的数组rowscolumns。代码需要查看数组并找到至少有一个负数的所有行。如果 found ,则代码zeros在所创建的行下方添加一个新行。

代码

#include <pch.h>
#include <iostream>

using namespace std;

int main()
{
int rows, columns;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> columns;


int **array = new int*[rows];                  //Generating a 2-D array
for (int i = 0; i < rows; i++)
    array[i] = new int[columns];

std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < columns; i++)              //loop for input array 
    for (int j = 0; j < rows; j++)             //elements
        std::cin >> array[i][j];

for (int i = 0; i < columns; i++) {            //print the array
    for (int j = 0; j < rows; j++) {
        std::cout << array[i][j] << " ";
    }
    std::cout << "\n";
}
for (int i = 0; i < columns; i++) {             //finding rows with negative
    for (int j = 0; j < rows; j++) {            //numbers and adding a new 
        if (array[i] < 0) {                     // row of zeros below
            array[i + 1][j] = 0;
            std::cout << array[i][j] << " ";
        }
    }
    std::cout << "\n";
}

return 0;
}

例如,
如果我们输入一个数组 ,答案应该是 但我的代码没有这样做?
1 1 1 1 1
2 -2 2 -2 2
3 3 3 3 3
4 -4 -4 4 4
5 5 5 5 5


1 1 1 1 1
2 -2 2 -2 2
0 0 0 0 0 -----> new rows added
3 3 3 3 3
4 -4 -4 4 4 ------> new rows added
0 0 0 0 0
5 5 5 5 5

标签: c++arraysc++11multidimensional-arrayjagged-arrays

解决方案


这个 main.cpp 产生所需的输出和一些监控。

看哪,结果数组,包括适当的零行,只是写入标准输出。但是,我为原始数组分配了两倍于该解决方案当前需要的空间。

如果需要,您可以支持此解决方案以对大数组进行就地重写,以在其上部包含完整的输出数组。

否则程序保持原样,数组大小的一半(1x 行乘以 1x 列)就足够了。

#include <iostream>

int main() {
    // The **wished for** array dimensions.
    int rows{0}, columns{0};

    std::cout << "Enter the number of rows: ";
    std::cin  >>  rows;

    std::cout << "Enter the number of columns: ";
    std::cin  >>  columns;

    // ------------------------------------------------- ALLOCATION START ----

    // Generating 2-D array.
    int ** array = new int* [2 * rows];  // (*)

    // (*) Suppose all the rows are bad, i. e., with some negative number(s),
    //     then **each and every** single row had to be followd by zeroes.

    // Allocating the columns.
    for (int i = 0; i < 2 * rows; i++) {
        array[i] = new int[columns];
    }


    // NEW --- NEW ---- NEW

    // Allocate bool array indicating that row[i] must be followed by zeroes. 
    bool* want_zeroes = new bool[rows];

    for (int i = 0; i < rows; i++) want_zeroes[i] = false;     // Initialize

    // ----------------------------------------------- ALLOCATION END -----

    std::cout << "-- -- -- -- \n";

    // --------------------------------------------------- INTAKE START ---

    std::cout << "Enter the elements" << std::endl;

    for (int i = 0; i < rows; i++)              //loop for input array 
        for (int j = 0; j < columns; j++)             //elements
            std::cin >> array[i][j];

    std::cout << "-- -- -- -- \n";

    std::cout << "Print the elements" << std::endl;

    // Monitor original array.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            std::cout << array[i][j] << " ";
        }

        std::cout << "\n";
    }

    // -------------------------------------------------- INTAKE END -------

    std::cout << "-- -- -- -- \n";

    // --------------------------------------------------- CHECK START ----

    std::cout << "Print want_zeroes" << std::endl;

    for (int i = 0; i < rows; i++) {             //finding rows with negative
        for (int j = 0; j < columns; j++) {            //numbers and adding a new 

            // Check for negative element.
            if (array[i][j] < 0) {

                // Boom, found a bad row!
                // So, this is row so-and-so,
                // which needs to be followed
                // by a row of zeroes later.

                want_zeroes[i] = true;
            }
        }

        // Monitor.
        std::cout << std::boolalpha << want_zeroes[i] << '\n';
    }

    // -------------------------------------------------- CHECK END ------

    std::cout << "-- -- -- -- \n";

    // -------------------------------------- CONSTRUCT OUTPUT START -----

    // We use the quick-and-dirty solution, not modifying the original
    // array, but just outputting the solution to stdout, see if it's ok.

    std::cout << "Print resulting array" << std::endl;

    for (int i = 0; i < rows; i++) {

        // Output current original row anyway. 
        for (int j = 0; j < columns; j++) {
            std::cout << array[i][j] << ' ';
        }

        std::cout << '\n';

        // Was this a bad row?
        if (want_zeroes[i]) {

            // Yes! -- Output a row of zeroes.
            for (int j = 0; j < columns; j++) {
                std::cout << 0 << ' ';
            }

            std::cout << '\n';
        }
     }

    // -------------------------------------- CONSTRUCT OUTPUT END -------


    // ------------------------------------------ DEALLOCATION START -----

    // Dealocate want_zerores.
    if (want_zeroes != nullptr) {
        delete[] want_zeroes;
        want_zeroes  = nullptr;
    }


    // Deallocate the array's columns.
    for (int i = 0; i < 2 * rows; i++) {
        if (array[i] != nullptr) {
            delete[] array[i];
            array[i]  = nullptr;
        }
    }

    // Dealocate array.
    if (array != nullptr) {
        delete[] array;
        array  = nullptr;
    }

    // ------------------------------------------- DEALLOCATION END -------

    return 0;
}

问候,米查


推荐阅读