Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Лабораторная для Гайделя №3 вариант 25
- #include<iostream>
- #include<vector>
- using namespace std;
- //ввод длины
- int inputRows()
- {
- cout << "input the number of rows: ";
- int rows;
- cin >> rows;
- cin.clear();
- if (cin.get() != '\n' || rows <= 0) {
- while (cin.get() != '\n');
- cout << "error inputing! try again" << endl;
- return inputRows();
- }
- return rows;
- }
- // ввод высоты
- int inputColumns()
- {
- cout << "input the number of columns: ";
- int columns;
- cin >> columns;
- cin.clear();
- if (cin.get() != '\n' || columns <= 0) {
- while (cin.get() != '\n');
- cout << "error inputing! try again" << endl;
- return inputColumns();
- }
- return columns;
- }
- //Продолжение
- bool Prod()
- {
- cout << "Continue Y/N" << endl;
- char yn;
- cin >> yn;
- cin.clear();
- if (yn == 'Y' && cin.get() == '\n') return true;
- else if (yn == 'N' && cin.get() == '\n') return false;
- else
- {
- while (cin.get() != '\n');
- cout << "Error. Try again" << endl;
- Prod();
- }
- }
- int main()
- {
- do {
- int rows = inputRows();
- int columns = inputColumns();
- vector < vector <int> > setVector(rows, vector <int>(columns));
- //заполнение массива
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- bool vecto = true;
- while (vecto) {
- cout << "input element (" << i + 1 << ", " << j + 1 << "): ";
- cin >> setVector[i][j];
- cin.clear();
- if (cin.get() != '\n' || setVector[i][j] < 0)
- {
- while (cin.get() != '\n') vecto = true;
- cout << "error inputing! try again" << endl;
- }
- else
- vecto = false;
- }
- }
- }
- cout << endl;
- //вывод массива
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- cout << setVector[i][j] << ' ';
- }
- cout << endl;
- }
- cout << endl << "result:" << endl;
- //сортировка строк матрицы
- for (int i = 0; i < rows - 1; i++)
- {
- for (int y = i + 1; y < rows; y++)
- {
- for (int j = 0; j < columns; j++)
- {
- if (setVector[i][j] > setVector[y][j])
- {
- for (int x = 0; x < columns; x++)
- swap(setVector[i][x], setVector[y][x]);
- break;
- }
- else if (setVector[i][j] < setVector[y][j]) {
- break;
- }
- }
- }
- }
- //вывод результата
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- cout << setVector[i][j] << ' ';
- }
- cout << endl;
- }
- } while (Prod());
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement