Bonjour,
J'ai écrit un programme qui compile parfaitement mais qui plante dès le debut ( .exe a cessé de fonctionner ).
La bibliothèque math.h n'a posé aucun problème pour toutes les compilations intermédiaires, donc je ne pense pas que le problème vienne de là.
Voici mon programme : si quelqu'un pense avoir une idée... un grand merci d'avance
Code:#include<stdio.h> #include<math.h> typedef struct square_matrix_s { int _size ; float values[100][100] ; } square_matrix ; typedef struct polynomial_s { int degree ; float coefficients[200] ; } polynomial ; typedef struct poly_square_matrix_s { int _size ; polynomial values[100][100] ; } poly_square_matrix ; int integermax(int a, int b){ if (a>b) return a ; else return b ; } poly_square_matrix characteristic_matrix (square_matrix M) { poly_square_matrix N ; N._size = M._size ; int i,j ; for (i=0;i<M._size;i++) { for (j=0;j<M._size;j++) { polynomial P ; P.degree = 0 ; P.coefficients[0] = M.values[i][j] ; if (i==j) { P.degree = 1 ; P.coefficients[1] = -1 ; } N.values[i][j]=P ; } } return N ; } polynomial poly_add (polynomial a, polynomial b) { polynomial sum ; sum.degree = integermax(a.degree,b.degree) ; int i ; for (i=0;i<=sum.degree;i++) { sum.coefficients[i] = a.coefficients[i] + b.coefficients[i] ; } return sum; } polynomial poly_multiply (polynomial a, polynomial b) { int i,j ; polynomial product ; product.degree = a.degree+b.degree ; for (i=0;i<=product.degree;i++){ product.coefficients[i]=0 ; } for (i=0;i<=a.degree;i++){ for (j=0;j<=b.degree;j++){ product.coefficients[i+j]=product.coefficients[i+j] + a.coefficients[i]*b.coefficients[j]; } } return product ; } char sign(float a) { if (a>=0) return '+' ; return '-' ; } void check_polynomial_degree(polynomial *P) { int i=P->degree ; while (P->coefficients[i]==0&&i>0) { i=i-1 ; } P->degree = i ; } float absolute (float a) { if (a<0) return -a ; else return a ; } void display_monomial(float a, int k, int deg) { if(k!=0) { printf(" %c ",sign(a)); } else { if (a<0) printf("-"); } if (pow(a,2)!=1) printf("%f",absolute(a)) ; else { if (deg==0) printf("1") ; } if (deg==1) printf("X"); else { if (deg>1) printf("X^%d",deg) ; } } void display_polynomial (polynomial P) { printf("\n"); if (P.degree==0&&P.coefficients[0]==0) printf("0"); else { int i,j=0 ; for (i=P.degree;i>=0;i=i-1) { if (P.coefficients[i]!=0) { display_monomial(P.coefficients[i],j,i); printf(" "); j++; } } } } void enter_polynomial (polynomial *P) { printf("\n\nWhat degree is the polynomial ? ") ; scanf("%d",&(P->degree)) ; int i ; for (i=0;i<=P->degree;i++) { if(i==0) { printf("\nEnter constant coefficient : ") ; } else { if (i==1) printf("\nX : "); else printf("\nX^%d : ",i); } scanf("%f",&(P->coefficients[i])) ; } check_polynomial_degree(P); printf("\nThe polynomial you entered is of degree %d and is ",P->degree); display_polynomial(*P) ; } void display_menu (int *b) { printf("\n\nWhat do you want to do ?") ; printf("\n1. Calculate determinant") ; printf("\n2. Calculate characteristic polynomial") ; printf("\n"); scanf("%d",b) ; } void display_matrix(square_matrix M) { int i,j ; printf("\n") ; for (i=0;i<M._size;i++) { for (j=0;j<M._size;j++) { printf("%f",M.values[i][j]) ; printf(" ") ; } printf("\n") ; } printf("\n") ; } void enter_matrix(square_matrix *M) { printf("\n\nWhat size is the matrix ? "); scanf("%i",&(M->_size)) ; int i,j ; printf("\nEnter the matrix¨s rows one after the other\nand press enter after each input: \n\n") ; for (i=0;i<M->_size;i++) { for (j=0;j<M->_size;j++) { scanf("%f",&(M->values[i][j])) ; } } } int placement(int offsquare, int newsquare) { if (offsquare<newsquare) return newsquare-1 ; else return newsquare ; } square_matrix reduce(square_matrix M, int a, int b) { square_matrix N ; N._size = M._size-1 ; int i,j ; for (i=0;i<M._size;i++) { for (j=0;j<M._size;j++) { if(i!=a&&j!=b) N.values[placement(a,i)][placement(b,j)]=M.values[i][j] ; } } return N ; } float determinant(square_matrix M) { float deter = 0 ; if (M._size != 1) { int i ; for (i=0;i<M._size;i++) { deter = deter + (pow((-1),i))*(M.values[i][0])*determinant(reduce(M,i,0)) ; } return deter ; } else return M.values[0][0] ; } polynomial poly_constant_multiply (int a, polynomial b) { polynomial P ; P.degree = 0 ; P.coefficients[0] = a ; return poly_multiply(P,b) ; } poly_square_matrix poly_reduce(poly_square_matrix M, int a, int b) { poly_square_matrix N ; N._size = M._size-1 ; int i,j ; for (i=0;i<M._size;i++) { for (j=0;j<M._size;j++) { if(i!=a&&j!=b) N.values[placement(a,i)][placement(b,j)]=M.values[i][j] ; } } return N ; } polynomial poly_determinant (poly_square_matrix M) { polynomial P ; P.degree = 0 ; P.coefficients[0]=0 ; if (M._size != 1) { int i ; for (i=0;i<M._size;i++) { P = poly_add(P,poly_constant_multiply((pow((-1),i)),poly_multiply(M.values[i][0],poly_determinant(poly_reduce(M,i,0))))) ; } return P ; } else return M.values[0][0] ; } int main (void) { int a = 1 ; int menu = 0 ; while(a>0) { display_menu(&menu) ; if (menu==1) { square_matrix matrix ; enter_matrix(&matrix); display_matrix(matrix) ; printf("\n\n%f",determinant(matrix)) ; } if (menu==2) { square_matrix matrix ; enter_matrix(&matrix); printf("The characteristic polynomial is "); display_polynomial(poly_determinant(characteristic_matrix(matrix))) ; } } return 0; }
-----