矩阵的行列式,C++代码疑难解答[已关闭]

o0lyfsai  于 2022-12-05  发布在  其他
关注(0)|答案(2)|浏览(145)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
23小时前关门了。
Improve this question

    • 主目录. 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;

}
    • 行列式. 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;
}
    • 行列式. h**
#ifndef DETERMINANT_H
#define DETERMINANT_H

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

void readMatrix(int**, int);

#endif
    • 生成文件**
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

我创建矩阵行列式的代码是无限循环的。显示的代码有什么问题?我相信它与main.c函数中的代码和初始化指针数组有关,但我不能指出错误在哪里。

lfapxunr

lfapxunr1#

您的includes有问题。您试图在.c文件中包含“iostream”,而iostream只用于c++。在c中,您可以导入<stdio.h>,这可以让您访问“printf”函数。您还导入了“string.h”,而您在程序中没有使用它。您还忘记了<stdlib.h>在main.c文件中包含,调用malloc需要它。
在determinant. c中,您还使用了名称空间。C不允许使用名称空间。
在编译时,你使用的是g++。g是用来编译c代码的。因为你的文件是用c写的,所以你应该使用gcc。

qv7cva1a

qv7cva1a2#

**您的域名已经到期,请联系您的服务商续费

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

或者

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

另一件事正如menno-schipper所说,你不应该在C头文件中包含C++头文件。

相关问题