Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- int main() {
- int rows = 3;
- int columns = 3;
- int **matrix = (int**) malloc(rows * sizeof(int*));
- for(int r = 0; r < rows; r++) matrix[r] = (int*) malloc(columns * sizeof(int));
- int item = 1;
- for(int r = 0; r < rows; r++) {
- for(int c = 0; c < columns; c++) {
- matrix[r][c] = item++;
- }
- }
- for(int r = 0; r < rows; r++) {
- for(int c = 0; c < columns; c++) {
- printf("%d ", matrix[r][c]);
- }
- printf("\n");
- }
- printf("\n");
- // Above Main diagonal
- for(int r = 0; r < rows; r++) {
- for(int c = 0; c < columns; c++) {
- if(c > r) printf("%d ", matrix[r][c]);
- }
- }
- printf("\n");
- // Below Main diagonal
- for(int r = 0; r < rows; r++) {
- for(int c = 0; c < columns; c++) {
- if(r > c) printf("%d ", matrix[r][c]);
- }
- }
- printf("\n");
- // Above Secondary diagonal
- for(int r = 0; r < rows; r++) {
- int c = rows - r - 2;
- while(c >= 0){
- printf("%d ", matrix[r][c]);
- c--;
- }
- }
- printf("\n");
- // // Below Secondary diagonal
- for(int r=1;r<rows;r++){
- for(int c=0;c<r; c++){
- printf("%d ",matrix[rows-c-1][r]);
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement