Created
March 4, 2021 15:41
-
-
Save jaskiratsingh2000/dc5df148f8bdc8cc547a7c08bfce03c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
struct dimensions{ | |
int rowsNumber; | |
int columnsNumber; | |
}; | |
struct dimensions readTwoIdenticalDimensions(); | |
void readMatrixDimentions(struct dimensions *dimensions, char name[]); | |
void matrixReadValues(int rows, int columns, int readInput[rows][columns]); | |
void matrixPrint(int rows, int columns, int readOutput[rows][columns]); | |
void readMatrixWithInstructions(int rows, int columns, int matrixInstructions[rows][columns], char name[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]; | |
struct dimensions d; | |
struct dimensions da, db; | |
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: | |
d = readTwoIdenticalDimensions(); | |
//Reading and printing the Matrices A and B | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixA, "A"); | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixB, "B"); | |
//Calling a function to Add the two matrices | |
printf("\t\nAdding the 2 matrices now, we get Matrix A + B:\n\n"); | |
matrixAddition(d.rowsNumber, d.columnsNumber, matrixA, matrixB); | |
break; | |
// Case 2 is for subtraction of the 2 matrices | |
case 2: | |
d = readTwoIdenticalDimensions(); | |
//Reading and printing the Matrices A and B | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixA, "A"); | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixB, "B"); | |
//Calling a function to subtract the two matrices | |
printf("\t\nSubtracting the 2 matrices now, we get Matrix A - Matrix B:\n\n"); | |
matrixSubtraction(d.rowsNumber, d.columnsNumber, matrixA, matrixB); | |
break; | |
//Case 3 is for addition of 2 matrices | |
case 3: | |
//when mulotiplying arrays matrixA colum # has to equal matrixB row # | |
readMatrixDimentions(&da, "A"); | |
readMatrixDimentions(&db, "B"); | |
// Column of first matrix should be equal to column of second matrix and | |
while (da.columnsNumber != db.rowsNumber) | |
{ | |
printf("\n\nError! column of first matrix not equal to row of second.\n\n"); | |
readDimentions(&da, "A"); | |
readDimentions(&db, "B"); | |
} | |
//Reading and printing the Matrices A and B | |
readMatrixWithInstructions(da.rowsNumber, da.columnsNumber, matrixA, "A"); | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixB, "B"); | |
//Calling a function to multiply the two matrices | |
printf("\t\n\tMultiplying the 2 matrices now:\n\n"); | |
matrixMultiplication(da.rowsNumber, da.columnsNumber, db.rowsNumber, da.columnsNumber, matrixA, matrixB); | |
//Case 4 is for doing the transpose of the matrix | |
case 4: | |
d = readTwoIdenticalDimensions(); | |
//Reading and printing the single matrix and here we will take matrix A | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixA, "A"); | |
printf("\t\nDoing the transpose of the above matrix:\n\n"); | |
matrixTransposePrint(d.rowsNumber, d.columnsNumber, matrixA); | |
break; | |
// Case 5 is for finding the determinant of a matrix | |
case 5: | |
d = readTwoIdenticalDimensions(); | |
//Reading and printing the single matrix and this case Matrix A | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixA, "A"); | |
printf("\t\n Finding the determinant of the above matrix:\n\n"); | |
int detRecievedFromFunction; | |
detRecievedFromFunction = matrixDeterminant(d.rowsNumber, d.columnsNumber, matrixA, rowsA); | |
printf("%d", detRecievedFromFunction); | |
break; | |
//Writing the Row Echeleon form | |
case 6: | |
d = readTwoIdenticalDimensions(); | |
//Reading and printing the Matrices A and B | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixA, "A"); | |
readMatrixWithInstructions(d.rowsNumber, d.columnsNumber, matrixB, "B"); | |
printf("\t\n Finding the RowElcheleon form of the above matrix:\n\n"); | |
//Calling a function to Add the two matrices | |
matrixRowEchleonForm(d.rowsNumber, d.columnsNumber, matrixA); | |
matrixPrint(d.rowsNumber, d.columnsNumber, matrixA); | |
break; | |
} | |
} | |
} | |
//Function to read the dimnesions of a matrix | |
void readMatrixDimentions(struct dimensions *d, char name[]){ | |
printf("\nEnter the #rows and #cols for matrix %s: ",name); | |
scanf("%d%d",&( d->rowsNumber ), &( d->columnsNumber )); | |
} | |
struct dimensions readTwoIdenticalDimensions(){ | |
struct dimensions da,db; | |
readMatrixDimentions(&da, "A"); | |
readMatrixDimentions(&db, "B"); | |
while ((da.rowsNumber != db.rowsNumber) || (da.columnsNumber != db.columnsNumber)){ | |
printf("\nMatrices must be the same size\n"); | |
readMatrixDimentions(&da, "A"); | |
readMatrixDimentions(&db, "B"); | |
} | |
return da; | |
} | |
//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"); | |
} | |
} | |
//Reading matrices with the intsructions | |
void readMatrixWithInstructions(int rows, int columns, int matrixInstructions[rows][columns], char name[rows][columns]){ | |
int readInputMatrix[rows][columns]; | |
printf("\n\tNow Let us enter the elements of Matrix %s with %d x %d matrix.\n\n", name, rows, columns);// with the %d we remember the user the dimentions of the array | |
matrixReadValues(rows, columns, readInputMatrix); | |
printf("\n\t\tMatrix %s\n\n",name); | |
matrixPrint(rows, columns, readInputMatrix); | |
} | |
//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