Untitled
unknown
plain_text
4 years ago
1.9 kB
7
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void matrix2D(int *mat, int r, int c); // Creates matrix
void displayMatrix(int *mat, int r, int c); // Display matrix in 2D
void transposeMatrix(int *mat, int r, int c); // Transpose matrix
int main(void)
{
int row, column;
printf("Please enter the size of row: ");
scanf("%d", &row);
printf("Please enter the size of column: ");
scanf("%d", &column);
int matrix[row][column];
matrix2D(matrix, row, column);
// puts("");
// for(int i = 0; i < row; i++){
// for(int j = 0; j < column; j++){
// printf("Matrix [%d][%d]: %d\n", i+1, j+1, matrix[i][j]);
// }
// }
puts("");
displayMatrix(matrix, row, column);
puts("");
transposeMatrix(matrix, row, column);
return 0;
}
void matrix2D(int *mat, int r, int c){
printf("Enter the elements row-by-row.\n");
int n = 0;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
printf("Matrix [%d][%d]: ", i+1, j+1);
int element;
scanf("%d", &element);
*(mat + n) = element;
n++;
}
}
}
void displayMatrix(int *mat, int r, int c){
printf("The matrix in 2D is shown as below.");
puts("");
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
printf("%d ", *(mat + i*r + j));
if(j == c-1){
printf("\n"); // Prints new line
}
}
}
}
void transposeMatrix(int *mat, int r, int c){
printf("The transpose of a matrix is shown below.");
puts("");
for(int i = 0; i < c; i++){
for(int j = 0; j < r; j++){
printf("%d ", *(mat + i*c + j));
}
printf("\n"); // Prints new line
}
}Editor is loading...