All pastes #2091769 Raw Edit

Miscellany

public cpp v1 · immutable
#2091769 ·published 2011-10-20 13:20 UTC
rendered paste body
#include <cstdio>#include <iostream>#include <iomanip>int main(){    int width = 0, height = 0, direction = 0;    do    {        std::cout << "Please, input array width: ";        std::cin >> width;        if (width > 0)            break;        std::cout << "Invalid value!" << std::endl;        std::cin.clear();        fflush(stdin);    } while (true);    do    {        std::cout << "Please, input array height: ";        std::cin >> height;        if (height > 0)            break;        std::cout << "Invalid value!" << std::endl;        std::cin.clear();        fflush(stdin);    }    while (true);    do    {        std::cout << "Please, sort direction (-1 or 1): ";        std::cin >> direction;        if (direction == -1 || direction == 1)            break;        std::cout << "Invalid value!" << std::endl;        std::cin.clear();        fflush(stdin);    } while (true);    int* array = new int[width * height];    std::cout << "Input array values:" << std::endl;    for (int i = 0; i < width; ++i)        for (int j = 0; j < height; ++j)        {            std::cout << "[" << i << ", " << j << "]: ";            std::cin >> array[i * width + j];        }    // sort    bool finded = true;    while (finded)    {        finded = false;        // sort horizontally        for (int i = 0; i < width - 1; ++i)            for (int j = 0; j < height; ++j)                if (direction < 0 && array[i * width + j] > array[(i + 1) * width + j] ||                    direction > 0 && array[i * width + j] < array[(i + 1) * width + j])                {                    int tmp = array[i * width + j];                    array[i * width + j] = array[(i + 1) * width + j];                    array[(i + 1) * width + j] = tmp;                    finded = true;                }        // sort vertically        for (int i = 0; i < width; ++i)            for (int j = 0; j < height - 1; ++j)                if (direction < 0 && array[i * width + j] > array[i * width + j + 1] ||                    direction > 0 && array[i * width + j] < array[i * width + j + 1])                {                    int tmp = array[i * width + j];                    array[i * width + j] = array[i * width + j + 1];                    array[i * width + j + 1] = tmp;                    finded = true;                }    }    // output    for (int j = 0; j < height; ++j)    {        for (int i = 0; i < width; ++i)            std::cout << std::setw(5) << array[i * width + j];                std::cout << std::endl;    }    delete[] array;    return EXIT_SUCCESS;}