Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Euklid
- void euklid(int a, int b){
- int r = a % b;
- while (r > 0){
- a = b;
- b = r;
- r = a % b;
- }
- cout << b << endl;
- }
- // Triangle of *
- void printTriangle(int n){
- for (int k = 1; k <= n; k++){
- for (j = 1; j <= n-k; j++){
- cout << " ";
- }
- for (i = 1; i <= 2*k-1; i++){
- cout << "*";
- }
- cout << endl;
- }
- }
- // Sum of diagonals of matrix
- void printDiagonalSums(int mat[][m], int n){
- int principal = 0, secondary = 0;
- for (int i = 0; i < n; i++){
- for (int j = 0; j < n; j++){
- // Condition for principal diagonal
- if (i == j){
- principal += mat[i][j];
- }
- // Condition for secondary diagonal
- if ((i + j) == (n - 1)){
- secondary += mat[i][j];
- }
- }
- }
- cout << "Principal diagonal: " << principal << endl;
- cout << "Secondary diagonal: " << secondary << endl;
- }
- // Inversions count in array
- int getInvCount(int &arr[], int n){
- int inv_count = 0;
- for (int i = 0; i < n - 1; i++){
- for (int j = i+1; j < n; j++){
- if (arr[i] > arr[j]){
- inv_count++;
- }
- }
- }
- return inv_count;
- }
- // Compute sum of 8 neighbours of element in 2D array
- #define ROWS 8
- #define COLS 8
- int sum(int &matrix, int row, int col){
- int sum = 0;
- for (int i = -1; i < 2; i++){
- for (int j = -1; j < 2; j++){
- // skip center cell
- if (i == j){
- continue;
- }
- // skip rows out of range
- if ((i + row) < 0 || (i + row >= ROWS){
- continue;
- }
- // skip columns out of range
- if ((j + col) < 0 || (j + col >= COLS){
- continue;
- }
- // add to sum
- sum += matrix[i + row][j + col];
- }
- }
- return sum;
- }
- int make(int &result, int &source){
- for (int i = 0; i < ROWS; i++){
- for (int j = 0; j < COLS; j++){
- result[i][j] = sum(source, i, j);
- }
- }
- }
- int result[ROWS][COLS] = {0};
- int source[ROWS][COLS] = {elements};
- make(result, source);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement