Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int main(){
- //matrix construction
- int n,m;
- scanf("%d %d", &n, &m);
- int **matrix = malloc(sizeof(int*) * n);
- for (int i = 0; i < n; i++)
- matrix[i] = malloc(sizeof(int) * m);
- //filling matrix with 0
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- matrix[i][j] = 0;
- //count for filling up the matrix
- int count = 1;
- //Fill upper left part
- for(int j = 0; j < m; j++)
- {
- int column = j;
- for(int row = 0; column >= 0; column--)
- {
- matrix[row][column] = count++;
- row++;
- }
- }
- //Fill bottom right part
- for(int i = 1; i < n; i++)
- {
- int row = i;
- for(int column = n-1; row < n; row++)
- {
- matrix[row][column] = count++;
- column--;
- }
- }
- //Printing matrix
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- printf(" %d", matrix[i][j]);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement