Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <cstring>
  5. using namespace std;
  6. 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.
  7. int x = 0;
  8. for (int i = 1; i < 10; i++) {
  9. for (int j=0; j < i; j++) {
  10. vec[x] = matrix[i][j];
  11. x++;
  12. }
  13. }
  14.  
  15. }
  16. void rigtupperarray(int matrix[][10], int vec[], int size){// A function that copies the values ​​from the matrix
  17. int x = 0;
  18. for (int i = 0; i < 9; i++) {
  19. for (int j = i + 1; j < 10; j++) {
  20. vec[x] = matrix[i][j];
  21. x++;
  22. }
  23. }
  24. }
  25. 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
  26. int first, i;
  27. for (first = 1; first < size; first++) {
  28. int num = vec[first];
  29. for (i = first; i > 0 && num < vec[i - 1]; i--)
  30. vec[i] = vec[i - 1];
  31. vec[i] = num;
  32. }
  33. }
  34.  
  35. int main() {
  36. int matrix[10][10]{};
  37. ifstream numbers("numbers.txt");//Statement and opening file
  38. if (!numbers) {//Check file integrity for opening
  39. cout << "ERROR" << endl;
  40. return 0;
  41. }
  42. int num;
  43. for (int i = 0; i < 10; i++) {//Matrix data absorption
  44. for (int j = 0; j < 10; j++) {
  45. numbers >> num;
  46. matrix[i][j] = num;
  47. }
  48. }
  49. cout << "before sorted:" << endl;//Print the matrix before the change
  50. for (int i = 0; i < 10; i++) {
  51. for (int j = 0; j < 10; j++) {
  52. cout << matrix[i][j]<<" ";
  53. }
  54. cout << endl;
  55. }
  56. int rigtarr[45] = { 0 };
  57. int leftarr[45] = { 0 };
  58. rigtupperarray(matrix,rigtarr,45);//Sending the matrix to the function of the right top triangle that enters the data into a new array
  59. insertsort(rigtarr, 45);//Sending the newly created array to a function that returns the array in sorted order by income sorting
  60. Leftupperarray(matrix, leftarr, 45);//Sending the matrix to the function of the lower left triangle and moving the data to a new array
  61. insertsort(leftarr, 45);//Sending the newly created array to a function that returns the array in sorted order by income sorting
  62. int x = 0;
  63. for (int i = 0; i < 9; i++) {//Rearranging the matrix, of the part of the right upper triangle according to the new ordered array.
  64. for (int j = i + 1; j < 10; j++) {
  65. matrix[i][j] = rigtarr[x];
  66. x++;
  67. }
  68.  
  69. }
  70. int y = 0;
  71. for (int i = 1; i < 10; i++) {//Rearranging the matrix, of the part of the lower left triangle according to the new ordered array.
  72. for (int j = 0; j < i; j++) {
  73. matrix[i][j] = leftarr[y];
  74. y++;
  75. }
  76.  
  77. }
  78. cout << endl;
  79. cout << "sorted matrix:" << endl;
  80. for (int i = 0; i < 10; i++) {//Print the new matrix after the change
  81. for (int j = 0; j < 10; j++)
  82. cout << matrix[i][j] << " ";
  83. cout << endl;
  84.  
  85. }
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement