Read Dimensions and Elements
In this step, you will learn how to read matrix dimensions and elements for matrix multiplication in C. We'll create a program that allows users to input the size and values of two matrices.
First, let's create a new C file for our matrix multiplication program:
cd ~/project
nano matrix_multiply.c
Now, add the following code to read matrix dimensions:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int rows1, cols1, rows2, cols2;
// Read dimensions of first matrix
printf("Enter dimensions of first matrix (rows columns): ");
scanf("%d %d", &rows1, &cols1);
// Read dimensions of second matrix
printf("Enter dimensions of second matrix (rows columns): ");
scanf("%d %d", &rows2, &cols2);
// Check if matrices can be multiplied
if (cols1 != rows2) {
printf("Matrix multiplication not possible!\n");
return 1;
}
printf("Matrix dimensions are valid for multiplication.\n");
return 0;
}
Compile and run the program:
gcc matrix_multiply.c -o matrix_multiply
./matrix_multiply
Example output:
Enter dimensions of first matrix (rows columns): 2 3
Enter dimensions of second matrix (rows columns): 3 2
Matrix dimensions are valid for multiplication.
Let's break down the code:
- We define
MAX_SIZE
as 100 to limit matrix dimensions
scanf()
is used to read matrix dimensions from user input
- We check if matrix multiplication is possible by comparing columns of first matrix with rows of second matrix
- If dimensions are incompatible, the program prints an error message
Now, let's modify the code to read matrix elements:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int rows1, cols1, rows2, cols2;
int matrix1[MAX_SIZE][MAX_SIZE];
int matrix2[MAX_SIZE][MAX_SIZE];
// Read dimensions of first matrix
printf("Enter dimensions of first matrix (rows columns): ");
scanf("%d %d", &rows1, &cols1);
// Read dimensions of second matrix
printf("Enter dimensions of second matrix (rows columns): ");
scanf("%d %d", &rows2, &cols2);
// Check if matrices can be multiplied
if (cols1 != rows2) {
printf("Matrix multiplication not possible!\n");
return 1;
}
// Read elements of first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
// Read elements of second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
printf("Matrices have been successfully read.\n");
return 0;
}
Compile and run the program:
gcc matrix_multiply.c -o matrix_multiply
./matrix_multiply
Example output:
Enter dimensions of first matrix (rows columns): 2 3
Enter dimensions of second matrix (rows columns): 3 2
Enter elements of first matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter elements of second matrix:
Enter element [0][0]: 7
Enter element [0][1]: 8
Enter element [1][0]: 9
Enter element [1][1]: 10
Enter element [2][0]: 11
Enter element [2][1]: 12
Matrices have been successfully read.