Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
void matrixReadValues(int rows, int columns, int readInput[rows][columns]);
void matrixPrint(int rows, int columns, int readOutput[rows][columns]);
void matrixAddition(int rows, int columns, int matrix1[rows][columns], int matrix2[rows][columns]);
void matrixSubtraction(int rows, int columns, int matrix1[rows][columns], int matrix2[rows][columns]);
void matrixMultiplication(int rowsA, int columnsA, int rowsB, int columnsB, int matrix1[rowsA][columnsA], int matrix2[rowsB][columnsB]);
void matrixTransposePrint(int rows, int columns, int matrixTranspose[rows][columns]);
int matrixDeterminant(int rows, int columns, int matrixDet[rows][columns], int matrixOrder);
void matrixRowEchleonForm(int rows, int columns, int matrix1[rows][columns]);
int main(void){
int operation; //used in swtich statements
char again = 'Y';
int rowsA, columnsA;
int rowsB, columnsB;
int matrixA[rowsA][columnsA];
int matrixB[rowsB][columnsB];
while (again == 'Y'){
//this is the operation menu just type A, B, C or D to calculate
printf("\n \t\t\tOperation Menu\n\n");
printf("\t1. to Add\n");
printf("\t2. to Subtract\n");
printf("\t3. to Multiply two matrices\n");
printf("\t4. to find the transpose of the matrix\n");
printf("\t5. to find the determinant of the matrix\n\n");
printf("\t6. to find the rowecheleon form of the matrix\n\n");
printf("\tEnter your choice: ");
scanf(" %d", &operation);
switch (operation){
//Case 1 is for addition of 2 matrices
case 1:
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
while ((rowsA != rowsB) && (columnsA != columnsB)){
printf("\n\tMatrices must be of the same size\n");
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
}
printf("\n\tNow Let us enter the elements of Matrix A %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix A\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\n\tNow Let us enter the elements of Matrix B %d x %d matrix.\n\n", rowsB, columnsB);
matrixReadValues(rowsB, columnsB, matrixB);
printf("\n\t\tMatrix B\n\n");
matrixPrint(rowsB, columnsB, matrixB);
printf("\t\nAdding the 2 matrices now, we get Matrix A + B:\n\n");
matrixAddition(rowsA, columnsA, matrixA, matrixB);
break;
// Case 2 is for subtraction of the 2 matrices
case 2:
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
while ((rowsA != rowsB) && (columnsA != columnsB)){
printf("\n\tMatrices must be of the same size\n");
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
}
printf("\n\tNow Let us enter the elements of Matrix A %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix A\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\n\tNow Let us enter the elements of Matrix B %d x %d matrix.\n\n", rowsB, columnsB);
matrixReadValues(rowsB, columnsB, matrixB);
printf("\n\t\tMatrix B\n\n");
matrixPrint(rowsB, columnsB, matrixB);
printf("\t\nSubtracting the 2 matrices now, we get Matrix A - Matrix B:\n\n");
matrixSubtraction(rowsA, columnsA, matrixA, matrixB);
break;
//Case 3 is for addition of 2 matrices
case 3:
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
while ((rowsA != rowsB) && (columnsA != columnsB)){
printf("\n\tMatrices must be of the same size\n");
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tEnter the #rows and #cols for matrix B: ");
scanf("%d%d", &rowsB, &columnsB);
}
printf("\n\tNow Let us enter the elements of Matrix A %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix A\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\n\tNow Let us enter the elements of Matrix B %d x %d matrix.\n\n", rowsB, columnsB);
matrixReadValues(rowsB, columnsB, matrixB);
printf("\n\t\tMatrix B\n\n");
matrixPrint(rowsB, columnsB, matrixB);
printf("\t\n\tMultiplying the 2 matrices now:\n\n");
matrixMultiplication(rowsA, columnsA, rowsB, columnsB, matrixA, matrixB);
//Adding the default statemnt if no option matches
default:
printf("\nIncorrect option! Please choose a number between 1-4.");
break;
//Case 4 is for doing the transpose of the matrix
case 4:
printf("\n\tEnter the #rows and #columns for matrix A: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tNow Let us enter the elements of Matrix %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\t\nDoing the transpose of the above matrix:\n\n");
matrixTransposePrint(rowsA, columnsA, matrixA);
break;
// Case 5 is for finding the determinant of a matrix
case 5:
printf("\n\tEnter the #rows and #columns for matrix A. Make sure you add the square matrix: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tNow Let us enter the elements of Matrix %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\t\n Finding the determinant of the above matrix:\n\n");
int detRecievedFromFunction;
detRecievedFromFunction = matrixDeterminant(rowsA, columnsA, matrixA, rowsA);
printf("%d", detRecievedFromFunction);
break;
//Writing the Row Echeleon form
case 6:
printf("\n\tEnter the #rows and #columns for matrix A. Make sure you add the square matrix: ");
scanf("%d%d", &rowsA, &columnsA);
printf("\n\tNow Let us enter the elements of Matrix %d x %d matrix.\n\n", rowsA, columnsA);
matrixReadValues(rowsA, columnsA, matrixA);
printf("\n\t\tMatrix\n\n");
matrixPrint(rowsA, columnsA, matrixA);
printf("\t\n Finding the RowElcheleon form of the above matrix:\n\n");
matrixRowEchleonForm(rowsA, columnsA, matrixA);
matrixPrint(rowsA, columnsA, matrixA);
break;
}
}
}
//Function to read the value from the users
void matrixReadValues(int rows, int columns, int readInput[rows][columns]){
int i,j;
for(i=0; i < rows; i++ ){
for(j=0; j < columns; j++){
printf("\tEnter the elemnts [%d][%d]: ", i+1, j+1);
scanf("%d", &readInput[i][j]);
}
}
}
//Printing the matrix values
void matrixPrint(int rows, int columns, int readOutput[rows][columns]){
int i,j;
for(i=0; i < rows; i++ ){
for(j=0; j < columns; j++){
printf("\t%d\t", readOutput[i][j]);
}
printf("\n");
}
}
//Function to add the 2 matrices
void matrixAddition(int rows, int columns, int matrix1[rows][columns], int matrix2[rows][columns]){
int sum[rows][columns];
int i,j;
for(i=0; i < rows; i++ ){
for(j=0; j < columns; j++){
sum[i][j] = matrix1[i][j] + matrix2[i][j];
printf("\t%d\t", sum[i][j]);
}
printf("\n");
}
}
//Function to subtract the 2 matrices
void matrixSubtraction(int rows, int columns, int matrix1[rows][columns], int matrix2[rows][columns]){
int difference[rows][columns];
int i,j;
for(i=0; i < rows; i++ ){
for(j=0; j < columns; j++){
difference[i][j] = matrix1[i][j] - matrix2[i][j];
printf("\t%d\t", difference[i][j]);
}
printf("\n");
}
}
//Functrion to multiply the 2 matrices
void matrixMultiplication(int rowsA, int columnsA, int rowsB, int columnsB, int matrix1[rowsA][columnsA], int matrix2[rowsB][columnsB]){
int multiply[rowsA][columnsB];
int i, j, k;
//Initializing all the elemnts of multiply to 0
for (i = 0; i<rowsA; ++i)
for (j = 0; j<columnsB; ++j)
{
multiply[i][j] = 0;
}
// Checking whether the user wants to do "AB" or "BA" multiplication
int options;
printf("\t What type of operation do you want to perform from A x B or B x A? <Write either 1 for A x B or 2 for B x A>" );
scanf("%d", &options);
if(options == 1){
// Running the loop for the multiplication of the 2 matrices
for (i = 0; i<rowsA; i++){
for (j = 0; j<columnsB; j++){
for (k = 0; k<columnsA; k++){
multiply[i][j] += matrix1[i][k] * matrix2[k][j];
}
printf("\t%d\t", multiply[i][j]);
}
printf("\n");
}
}
else if( options == 2){
// Running the loop for the multiplication of the 2 matrices
for (i = 0; i<rowsB; i++){
for (j = 0; j<columnsA; j++){
for (k = 0; k<columnsB; k++){
multiply[i][j] += matrix2[i][k] * matrix1[k][j];
}
printf("\t%d\t", multiply[i][j]);
}
printf("\n");
}
}
else {
printf("Please add the appropiate values:");
printf("What type of operation do you want to perform from A x B or B x A? <Write either 1 for A x B or 2 for B x A>" );
scanf("%d", &options);
}
}
//Printing the transpose matrix values
void matrixTransposePrint(int rows, int columns, int matrixTranspose[rows][columns]){
int i,j;
for(i=0; i < rows; i++ ){
for(j=0; j < columns; j++){
printf("\t%d\t", matrixTranspose[j][i]);
}
printf("\n");
}
}
//Printing the Determinant of Matrix
int matrixDeterminant(int rows, int columns, int matrixDet[rows][columns], int matrixOrder){
int determinant=0, currentColumn, s = 1, i, j, m, n;
int subMatrixDet[rows][columns]; //This is the matrix which extracted from the enterred matrix (matrixDet[rows][columns]) as a sub matrix
if(matrixOrder == 1){
return(matrixDet[0][0]);
}
else{
// We would be applying the loop to perform the operation for every column
for(currentColumn = 0; currentColumn < matrixOrder - 1; currentColumn++){
m = 0, n = 0; //We initialized it because everytime loop will run we will extract new determinant
//Loop for writing/extracting the sub matrix from the original matrix in order to calculate the minor
for(i=0; i < matrixOrder; i++){
for(j=0; j < matrixOrder; j++){
subMatrixDet[i][j] = 0;
//Since we have to exclude the element which we multiply with the sub determinant matrix
if(i !=0 && j !=0 ){
subMatrixDet[m][n] = matrixDet[i][j];
//Incrementing the Value of n because first the different columns gets filled.
if(n < (matrixOrder - 2)){
n++;
}
else{
n=0;
m++;
}
}
}
}
}
determinant = determinant + s*(matrixDet[0][currentColumn] * matrixDeterminant(rows, columns, subMatrixDet, matrixOrder-1) );
s = -1 * s;
}
return determinant;
}
// Converting in the row echleon form
void matrixRowEchleonForm(int rows, int columns, int matrix1[rows][columns]){
int i,j, nextRow;
int firstElement;
int firstElementNextRow;
for(i = 0; i < rows; i++){
if(matrix1[i][i] != 1){
firstElement = matrix1[i][i];
//Checking if the furst element is the 0
if(firstElement == 0){
continue; //We are avoiding to divide the first element by 0
}
//Now dividing the specific row with different column number by the First element of the row
for(j=0; j < columns ; j++){
matrix1[i][j] = matrix1[i][j] / firstElement;
}
}
for(nextRow = i + 1; nextRow < rows; nextRow++){
//We are now subtracting the next row with the previous row in order to make the very first elements 0
firstElementNextRow = matrix1[nextRow][i];
for(j=0; j < columns; j++){
matrix1[nextRow][j] = matrix1[nextRow][j] - (matrix1[i][j] * firstElementNextRow);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment