Untitled
unknown
plain_text
4 years ago
1.7 kB
4
Indexable
#include <stdio.h> #include <stdlib.h> int * array2D(int r, int c); void arrayPrint(int r, int c, int *ptr); void matrixTranspose(int r, int c, int *ptr); 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); arrayPrint(row, column, ptr); matrixTranspose(row, column, ptr); // for(int i = 0; i < row; i++){ // for(int j = 0; j < column; j++){ // printf("Array[%d][%d] = %d\n", i, j, *(ptr + i*100 + j)); // k++; // } // } 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, j); 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 + i*100 + j)); } printf("\n"); } }
Editor is loading...