How to output an array (in C or C ++) with a specific parameter in a column. For example: there is an array from -100 to 100, output the array in this order: negative elements to the left, plus to the right, and zeros in the middle.

    2 answers 2

    int chooseColumn(int value) { if (value < 0) { return 0; } else if (value == 0) { return 1; } else { return 2; } } std::vector<std::vector<int>> columns(3); int values[size]; ... for (size_t i = 0; i < size; ++i) { columns[chooseColumn(value[i])].push_back(value[i]); } size_t maxColumnSize = 0; for (int i = 0; i < 3; ++i) { maxColumnSize = std::max(maxColumnSize, columns[i].size()); } for (int line = 0; i < maxColumnSize; ++i) { for (int column = 0; column < 3; ++column) { if (columns[column].size() < line) { std::cout << column[column][line]; } std::cout << '\t'; } std::cout << std::endl; } 
    • one
      Is it possible in C, but I did not understand in C ++? - Endru
    • one
      In C, instead of vector, there will be arrays that need to be dynamically changed. Instead of cout will be printf. What do not you understand? - dzhioev

    I would use a matrix in such a problem and not a linear array. Then the answer is simple, and it's not necessary to break your head.