Untitled

 avatar
unknown
plain_text
4 years ago
1.7 kB
9
Indexable
#include <stdio.h>
#include <stdlib.h>

int * array2D(int r, int c); // A function to obtain a 2-D matrix
void arrayPrint(int r, int c, int *ptr); // A function to print the 2-D matrix
void matrixTranspose(int r, int c, int *ptr); // A function to print the transpose of the 2-D matrix

int main(void)
{
    int stu_id = 70357003;

    int row, column;
    printf("Enter the size of row: ");
    scanf("%d", &row);
    printf("Enter the size of column: ");
    scanf("%d", &column);

    int *ptr, k = 0;
    ptr = array2D(row, column);
    printf("\n");
    arrayPrint(row, column, ptr);
    puts("");
    matrixTranspose(row, column, ptr);

    printf("\n\nStudent ID: %d\n\n", stu_id);
    return 0;
}

int * array2D(int r, int c){
    static int matrix_2d[100][100];
    int element;
    printf("Please enter the elements row-by-row: \n");

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("Matrix [%d][%d]: ", i+1, j+1);
            scanf("%d", &element);
            matrix_2d[i][j] = element;
        }
    }
    return matrix_2d;
}

void arrayPrint(int r, int c, int *ptr){
    printf("Matrix in 2-D format:\n");
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("%d ", *(ptr + i*100 + j));
            if(j == c - 1){
                printf("\n");
            }
        }
    }
}

void matrixTranspose(int r, int c, int *ptr){
    printf("The transpose of the 2-D matrix is:\n");
    for(int i = 0; i < c; i++){
        for(int j = 0; j < r; j++){
            printf("%d ", *(ptr + j*100 + i));
        }
        printf("\n");
    }
}
Editor is loading...