Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Test C++.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <vector>
- #include <iostream>
- using namespace std;
- // !!!
- // Write function Sort, that will sort the rows of matrix in ascending order.
- // Sorting must be done in-place.
- void SortMatrix(vector<vector<int>> & matrix)
- {
- int rows = matrix.size();
- int cols = matrix[0].size();
- size_t size = rows * cols;
- int* ar = new int[size];
- int count = 0;
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- int index = i * cols + j;
- ar[i * cols + j] = matrix[i][j];
- count++;
- }
- }
- //sort
- for (size_t i = 0; i < size; i++)
- {
- for (size_t j = 0; j < size - i - 1; j++)
- {
- if (ar[j] > ar[j + 1])
- std::swap(ar[j], ar[j + 1]);
- }
- }
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- int index = i * cols + j;
- matrix[i][j] = ar[i * cols + j];
- count++;
- }
- }
- cout << endl;
- delete[] ar;
- }
- void PrintMatrix(vector<vector<int>> & matrix)
- {
- for (auto & row : matrix) {
- for (auto & cell : row) {
- cout << cell << "\t";
- }
- cout << endl;
- }
- cout << endl;
- }
- void Sort()
- {
- int ar[10] = {6, 8, 4, 3, 9, 7, 0, 1, 2, 5};
- size_t size = 10;
- for (size_t i = 0; i < size; i++)
- {
- for (size_t j = 0; j < size - i - 1; j++)
- {
- if(ar[j] > ar[j + 1])
- std::swap(ar[j], ar[j + 1]);
- }
- }
- for (size_t i = 0; i < size; i++)
- cout << ar[i] << ' ';
- cout << endl;
- }
- int main()
- {
- vector<vector<int>> m1 =
- {
- { 9, 8 },
- { 1, 12 },
- { 3, 5 },
- };
- vector<vector<int>> m2 =
- {
- { 9, 8, 6 },
- { 4, 12, 2 },
- { 3, 5, 11 },
- };
- SortMatrix(m1);
- PrintMatrix(m1);
- SortMatrix(m2);
- PrintMatrix(m2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement