main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
int main(){
//Prompt the user for the size of matrix to be calculated.
printf("Welcome to the matrix determinant calculator!\n\n");
printf("Please select the matrix size you would like to input: \n");
printf("\t (A): 2x2 matrix\n");
printf("\t (B): 3x3 matrix\n\n");
char selection; //Stores matrix size selection
scanf(" %c", &selection);
int size; //Size of matrix
//Uses selection from user to determine value to assign to 'size'
if (selection == 'A' || selection == 'a'){
size = 2;
}
else if (selection == 'B' || selection == 'b'){
size = 3;
}
else {
printf("Your selection is invalid. Please start over.\n");
return 0;
}
printf("\nYou have selected a %dx%d matrix.\n\n", size, size);
//Initialize pointer array
int** matrix = (int**)malloc(size*sizeof(int*));
for (int i = 0; i < size; i++){
matrix[i] = (int*)malloc(size * sizeof(int));
}
readMatrix(matrix, size); //Sets up matrix by taking input from user
int calc = determinant(matrix, size); //Calculates determinant
printf("The %dx%d matrix is: \n\n", size, size);
//Displays the matrix on the console
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
printf("%d\t", matrix[row][col]);
}
printf("\n");
}
//Deletes stored data
for (int i = 0; i < size; i++){
free(matrix[i]);
}
free(matrix);
printf("\nThe determinant of the matrix is: %d\n", calc);
return 0;
}
determinant.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include "determinant.h"
int determinant(int** matrix, int size){
int detm_calc; //Determinant calculation variable
//Determine which formula to use - 2x2 or 3x3 matrix.
if (size == 2){ //2x2 case
int a = matrix[0][0];
int b = matrix[0][1];
int c = matrix[1][0];
int d = matrix[1][1];
detm_calc = (a*d) - (b*c);
}
else{ //3x3 case
int a = matrix[0][0];
int b = matrix[0][1];
int c = matrix[0][2];
int d = matrix[1][0];
int e = matrix[1][1];
int f = matrix[1][2];
int g = matrix[2][0];
int h = matrix[2][1];
int i = matrix[2][2];
detm_calc = a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g);
}
return detm_calc;
}
determinant.h
#ifndef DETERMINANT_H
#define DETERMINANT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
int determinant(int**, int);
#endif
matrix.c
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "determinant.h"
void readMatrix(int** matrix, int size){
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
printf("Please enter the integer for row %d column %d:\t", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
printf("\n");
}
}
matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "determinant.h"
void readMatrix(int**, int);
#endif
Makefile
determinant: main.o determinant.o matrix.o
gcc main.o determinant.o matrix.o -o determinant.out
main.o: main.c
gcc -c main.c
determinant.o: determinant.c determinant.h
gcc -c determinant.c
matrix.o: matrix.c matrix.h
gcc -c matrix.c
The code shown above is supposed to process the determinant of a matrix. Similar to https://www.mathsisfun.com/algebra/matrix-determinant.html It is a coding assignment however, when I turn it in, the code takes an extensive amount of time to process that the grading system intentional executes. There are no error results so I assume the code is infinitly looping due to some recursion error? I cannot spot any notable errors so I would love to get a second opinion. I will also provide the examples provided in the assignment below.
Examples In the examples below user input has been italicized. You don't have to do any italicizing in your program. It is just there to help you differentiate between what is input and what is output.
Example 1 Assume the file 3X3Matrix.txt had the following contents
3 3 1 2 3 4 5 6 7 8 9 ./findDeterminant.out 3X3Matrix.txt
The determinant is 0.00
Thank you for helping, I really appreciate it!