Untitled
unknown
plain_text
4 years ago
1.4 kB
8
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void matrix2D(int *mat, int r, int c);
void displayMatrix(int mat[], int r, int c); // Display matrix in 2D
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);
// 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);
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][j]);
if(j == c-1){
printf("\n"); // Prints new line
}
}
}
}Editor is loading...