// Program done on 25/01/2024 by AMALJITH A
// To develop a C program for matrix calculation in arduino and show the resultant matrix in the serial monitor
//the input for Matrix A and Matrix B is given through serial monitor input and the Resultant Matrix is displayed in serial monitor.
// Assign a max_size for your matrix according to your requirement
const int MAX_SIZE = 10;
//Variable declaration to store the dimension of matrices A and B
int rowsA, colsA, rowsB, colsB;
void setup() {
//to initialize serial communication
Serial.begin(9600);
}
void loop() {
// Input matrix dimensions
Serial.println("Enter the dimensions of matrix A (rows x columns): ");
readMatrixDimensions(rowsA, colsA);
Serial.println("Enter the dimensions of matrix B (rows x columns): ");
readMatrixDimensions(rowsB, colsB);
// Check if dimensions are valid for multiplication
if (colsA != rowsB) {
Serial.println("Error: Column of matrix A must be equal to the rows of matrix B.");
return;
}
// Input elements for matrix A
int matrixA[MAX_SIZE][MAX_SIZE];
Serial.println("Enter elements for matrix A:");
if (!inputMatrix(matrixA, rowsA, colsA)) {
return; // Error occurred
}
// Input elements for matrix B
int matrixB[MAX_SIZE][MAX_SIZE];
Serial.println("Enter elements for matrix B:");
if (!inputMatrix(matrixB, rowsB, colsB)) {
return; // Error occurred
}
// Display matrices
displayMatrix(matrixA, rowsA, colsA);
displayMatrix(matrixB, rowsB, colsB);
// Multiply matrices
int result[MAX_SIZE][MAX_SIZE];
multiplyMatrices(matrixA, matrixB, result);
// Display result
displayMatrix(result, rowsA, colsB);
}
// Function to input matrix dimensions and to store the dimension of matrices given through serial monitor
void readMatrixDimensions(int &rows, int &cols) {
while (!Serial.available());
String input = Serial.readStringUntil('\n');
sscanf(input.c_str(), "%d %d", &rows, &cols);
}
// Function to input elements for Matrices
bool inputMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
int elementsExpected = rows * cols;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
while (!Serial.available());
if (Serial.peek() == '\n') {
Serial.read(); //To consume newline
return false;
}
matrix[i][j] = Serial.parseInt();
if (Serial.available() && Serial.peek() == '\n') {
Serial.read(); //To consume newline
}
}
}
// to print the error if the user entered more elements than the size of a matrix
if (Serial.available()) {
Serial.println("Error: Extra characters after the matrix input.");
return false;
}
return true;
}
// Function to print and display the matrices
void displayMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
Serial.println("Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Serial.print(matrix[i][j]);
Serial.print("\t");
}
Serial.println();
}
Serial.println();
}
// Function for matrix multiplication
void multiplyMatrices(int A[MAX_SIZE][MAX_SIZE], int B[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE]) {
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}