Advertisement
Kostiggig

Bulshit 2

Jun 16th, 2023
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int main() {
  6.  
  7.     int rows = 3;
  8.     int columns = 3;
  9.     int **matrix = (int**) malloc(rows * sizeof(int*));
  10.  
  11.     for(int r = 0; r < rows; r++) matrix[r] = (int*) malloc(columns * sizeof(int));
  12.  
  13.     int item = 1;
  14.     for(int r = 0; r < rows; r++) {
  15.         for(int c = 0; c < columns; c++) {
  16.             matrix[r][c] = item++;
  17.         }
  18.     }
  19.  
  20.    
  21.     for(int r = 0; r < rows; r++) {
  22.         for(int c = 0; c < columns; c++) {
  23.             printf("%d ", matrix[r][c]);
  24.         }
  25.         printf("\n");
  26.     }
  27.  
  28.     printf("\nSecondary one\n");
  29.     int c = 0;
  30.     for(int r = 0; r < rows; r++) {
  31.         for(int c = 0; c < columns; c++) {
  32.             if(r + c == rows - 1) printf("%d ", matrix[r][c]);
  33.         }
  34.     }
  35.  
  36.     printf("\n");
  37.    
  38.  
  39.     // Above Main diagonal
  40.     for(int r = 0; r < rows; r++) {
  41.         for(int c = 0; c < columns; c++) {
  42.             if(c > r) printf("%d ", matrix[r][c]);
  43.         }
  44.     }
  45.  
  46.     printf("\n");
  47.  
  48.     // Below Main diagonal
  49.     for(int r = 0; r < rows; r++) {
  50.         for(int c = 0; c < columns; c++) {
  51.             if(r > c) printf("%d ", matrix[r][c]);
  52.         }
  53.     }
  54.  
  55.     printf("\n");
  56.  
  57.     // Above Secondary diagonal
  58.     for(int r = 0; r < rows; r++) {
  59.         int c = rows - r - 2;
  60.         while(c >= 0){
  61.             printf("%d ", matrix[r][c]);
  62.             c--;
  63.         }
  64.            
  65.     }
  66.  
  67.     printf("\n");
  68.  
  69.     // Below Secondary diagonal
  70.     for(int r=1;r<rows;r++){
  71.         for(int c=0;c<r; c++){
  72.             printf("%d ",matrix[rows-c-1][r]);
  73.          }
  74.       }
  75.    
  76.     printf("\n");
  77.    
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement