Home > Back-end >  Determinant of a Matrix, C code troubleshooting
Determinant of a Matrix, C code troubleshooting

Time:12-02

main.c

#include <stdio.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 <iostream>
#include <string>
#include "matrix.h"
#include "determinant.h"

using namespace std;

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 <string>
#include <iostream>
#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 <string>
#include <iostream>
#include "determinant.h"

void readMatrix(int**, int);

#endif

Makefile

determinant: main.o determinant.o matrix.o
    g   main.o determinant.o matrix.o -o determinant.out

main.o: main.c
    g   -c main.c

determinant.o: determinant.c determinant.h
    g   -c determinant.c

matrix.o: matrix.c matrix.h
    g   -c matrix.c

My code to create a determinant of a matrix is infinitely looping. What is wrong with the code shown? I believe it has something to do with the code within the main.c function and Initializing the pointer array but I can't pinpoint what is wrong.

CodePudding user response:

There is something wrong with your includes. You are trying to include 'iostream' in a .c file, while iostream is only used for c . In c, you can import <stdio.h>, which gives you access to the 'printf' function. You are also importing 'string.h', which you are not using in your program. You also forgot to include <stdlib.h> in your main.c file, which is needed to call malloc.

In determinant.c, you are also using a namespace. C does not allow namespaces.

While compiling, you are using g . g is used to compile c code. Since your files are written in c, you should use gcc.

CodePudding user response:

How you allocat ** is not the best way int** matrix = (int**)malloc(size * sizeof(int*)); Changed to:

int **matrix;
matrix = malloc( sizeof  *matrix * 3 ); // Allocate mem space for 3 [int*]
*matrix = malloc( sizeof **matrix * 3 ); // Allocate mem space for 3 [int]

Or

int* matrix_ptr = (int*) malloc(size * sizeof(int*));
int** matrix = &matrix_ptr;

Another thing as menno-schipper said, you should not include C headers in a C header file.

  • Related