Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main(){
  5.  
  6.     //matrix construction
  7.     int n,m;
  8.     scanf("%d %d", &n, &m);
  9.     int **matrix = malloc(sizeof(int*) * n);
  10.     for (int i = 0; i < n; i++)
  11.         matrix[i] = malloc(sizeof(int) * m);
  12.        
  13.     //filling matrix with 0
  14.     for (int i = 0; i < n; i++)
  15.         for (int j = 0; j < m; j++)
  16.             matrix[i][j] = 0;
  17.    
  18.     //count for filling up the matrix
  19.     int count = 1;
  20.    
  21.     //Fill upper left part
  22.     for(int j = 0; j < m; j++)
  23.     {
  24.         int column = j;
  25.         for(int row = 0; column >= 0; column--)
  26.         {
  27.             matrix[row][column] = count++;
  28.             row++;
  29.         }
  30.     }
  31.    
  32.     //Fill bottom right part
  33.     for(int i = 1; i < n; i++)
  34.     {
  35.         int row = i;
  36.         for(int column = n-1; row < n; row++)
  37.         {
  38.             matrix[row][column] = count++;
  39.             column--;
  40.         }
  41.     }
  42.    
  43.     //Printing matrix
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         for (int j = 0; j < m; j++)
  47.             printf(" %d", matrix[i][j]);
  48.         printf("\n");
  49.     }
  50.    
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement