在2D数组中添加行

时间:2018-11-19 19:34:23

标签: c++ arrays c++11 multidimensional-array jagged-arrays

用户输入所需的rowscolumns的所需数组。该代码需要遍历该数组,并找到至少具有一个负数的所有行。如果找到,则代码在已创建的行下方添加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

但是我的代码不行吗?

1 个答案:

答案 0 :(得分:0)

此main.cpp产生所需的输出以及一些监视。

看,结果数组(包括适当的零行)仅被写入stdout。但是,我为原始阵列分配的空间是此解决方案当前所需空间的两倍。

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

程序保持原样,否则,数组大小的一半(1x行x 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;
}

问候,米夏