Advertisement
Koragg

Algorithms (Matrix Stuff)

Jun 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. // Euklid
  2. void euklid(int a, int b){
  3.     int r = a % b;
  4.     while (r > 0){
  5.         a = b;
  6.         b = r;
  7.         r = a % b;
  8.     }
  9.     cout << b << endl;
  10. }
  11.  
  12. // Triangle of *
  13. void printTriangle(int n){
  14.     for (int k = 1; k <= n; k++){
  15.         for (j = 1; j <= n-k; j++){
  16.             cout << " ";
  17.         }
  18.         for (i = 1; i <= 2*k-1; i++){
  19.             cout << "*";
  20.         }
  21.         cout << endl;
  22.     }
  23. }
  24.  
  25. // Sum of diagonals of matrix
  26. void printDiagonalSums(int mat[][m], int n){
  27.     int principal = 0, secondary = 0;
  28.     for (int i = 0; i < n; i++){
  29.         for (int j = 0; j < n; j++){
  30.             // Condition for principal diagonal
  31.             if (i == j){
  32.                 principal += mat[i][j];
  33.             }
  34.             // Condition for secondary diagonal
  35.             if ((i + j) == (n - 1)){
  36.                 secondary += mat[i][j];
  37.             }
  38.         }
  39.     }
  40.     cout << "Principal diagonal: " << principal << endl;
  41.     cout << "Secondary diagonal: " << secondary << endl;
  42. }
  43.  
  44. // Inversions count in array
  45. int getInvCount(int &arr[], int n){
  46.     int inv_count = 0;
  47.     for (int i = 0; i < n - 1; i++){
  48.         for (int j = i+1; j < n; j++){
  49.             if (arr[i] > arr[j]){
  50.                 inv_count++;
  51.             }
  52.         }
  53.     }
  54.     return inv_count;
  55. }
  56.  
  57. // Compute sum of 8 neighbours of element in 2D array
  58. #define ROWS 8
  59. #define COLS 8
  60. int sum(int &matrix, int row, int col){
  61.     int sum = 0;
  62.     for (int i = -1; i < 2; i++){
  63.         for (int j = -1; j < 2; j++){
  64.             // skip center cell
  65.             if (i == j){
  66.                 continue;
  67.             }
  68.             // skip rows out of range
  69.             if ((i + row) < 0 || (i + row >= ROWS){
  70.                 continue;
  71.             }
  72.             // skip columns out of range
  73.             if ((j + col) < 0 || (j + col >= COLS){
  74.                 continue;
  75.             }
  76.             // add to sum
  77.             sum += matrix[i + row][j + col];
  78.         }
  79.     }
  80.     return sum;
  81. }
  82.  
  83. int make(int &result, int &source){
  84.     for (int i = 0; i < ROWS; i++){
  85.         for (int j = 0; j < COLS; j++){
  86.             result[i][j] = sum(source, i, j);
  87.         }
  88.     }
  89. }
  90.  
  91. int result[ROWS][COLS] = {0};
  92. int source[ROWS][COLS] = {elements};
  93. make(result, source);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement