Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #include <cstring>
- using namespace std;
- void Leftupperarray(int matrix[][10],int vec[],int size){//A function that copies the values ββfrom the matrix in the lower left triangle area into an array.
- int x = 0;
- for (int i = 1; i < 10; i++) {
- for (int j=0; j < i; j++) {
- vec[x] = matrix[i][j];
- x++;
- }
- }
- }
- void rigtupperarray(int matrix[][10], int vec[], int size){// A function that copies the values ββfrom the matrix
- int x = 0;
- for (int i = 0; i < 9; i++) {
- for (int j = i + 1; j < 10; j++) {
- vec[x] = matrix[i][j];
- x++;
- }
- }
- }
- void insertsort(int vec[], int size) {//Defining a function that receives an array and with the help of income sorting will arrange the array in a non-descending order
- int first, i;
- for (first = 1; first < size; first++) {
- int num = vec[first];
- for (i = first; i > 0 && num < vec[i - 1]; i--)
- vec[i] = vec[i - 1];
- vec[i] = num;
- }
- }
- int main() {
- int matrix[10][10]{};
- ifstream numbers("numbers.txt");//Statement and opening file
- if (!numbers) {//Check file integrity for opening
- cout << "ERROR" << endl;
- return 0;
- }
- int num;
- for (int i = 0; i < 10; i++) {//Matrix data absorption
- for (int j = 0; j < 10; j++) {
- numbers >> num;
- matrix[i][j] = num;
- }
- }
- cout << "before sorted:" << endl;//Print the matrix before the change
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- cout << matrix[i][j]<<" ";
- }
- cout << endl;
- }
- int rigtarr[45] = { 0 };
- int leftarr[45] = { 0 };
- rigtupperarray(matrix,rigtarr,45);//Sending the matrix to the function of the right top triangle that enters the data into a new array
- insertsort(rigtarr, 45);//Sending the newly created array to a function that returns the array in sorted order by income sorting
- Leftupperarray(matrix, leftarr, 45);//Sending the matrix to the function of the lower left triangle and moving the data to a new array
- insertsort(leftarr, 45);//Sending the newly created array to a function that returns the array in sorted order by income sorting
- int x = 0;
- for (int i = 0; i < 9; i++) {//Rearranging the matrix, of the part of the right upper triangle according to the new ordered array.
- for (int j = i + 1; j < 10; j++) {
- matrix[i][j] = rigtarr[x];
- x++;
- }
- }
- int y = 0;
- for (int i = 1; i < 10; i++) {//Rearranging the matrix, of the part of the lower left triangle according to the new ordered array.
- for (int j = 0; j < i; j++) {
- matrix[i][j] = leftarr[y];
- y++;
- }
- }
- cout << endl;
- cout << "sorted matrix:" << endl;
- for (int i = 0; i < 10; i++) {//Print the new matrix after the change
- for (int j = 0; j < 10; j++)
- cout << matrix[i][j] << " ";
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement