C中矩阵的行列式,故障排除



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!nn");
printf("Please select the matrix size you would like to input: n");
printf("t (A): 2x2 matrixn");
printf("t (B): 3x3 matrixnn");

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.nn", size, size);

//Initialize pointer array
int* matrix_ptr = (int*) malloc(size * sizeof(int*));
int** matrix = &matrix_ptr;
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: nn", size, size);

//Displays the matrix on the console
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
printf("%dt", 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: %dn", calc);

return 0;
}

行列式.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;
}

行列式.h

#ifndef DETERMINANT_H
#define DETERMINANT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
int determinant(int**, int);
#endif

矩阵.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");
}
}

矩阵.h

#ifndef MATRIX_H
#define MATRIX_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "determinant.h"
void readMatrix(int**, int);
#endif

生成文件

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

上面显示的代码本应创建矩阵的行列式,但由于它无限循环,因此处理不正确。我认为Makefile或从C++到C的翻译有问题。是否有我无法发现的显著错误?谢谢

您正在使用

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

问题:

  1. matrix_ptr初始化为指向int的指针,但为size*sizeof(int*)分配内存,就好像它是int*的数组一样。因此,分配的内存将取决于您的硬件,并且可能与int内存大小不匹配,导致内存分配大小错误
  2. int** matrix = &matrix_ptr;导致matrix指向matrix_ptr,因此当您试图在循环中写入matrix[1]时,您正试图写入未分配的内存块,这将导致分段故障

int** matrix = (int**)malloc(size*sizeof(int*));而不是这两条线将解决您的问题

注意:我不明白它在哪里循环(我在你的代码中遇到了segfault),也不明白makefile如何在你的情况下导致运行时问题。

最新更新