Hello, I decided to remake the code for the Kramer method. More precisely, it is better to issue and enter a condition if the determinant of the matrix is ​​0. But even before the introduction of this condition, an error occurred. I compile the code, enter values ​​from the keyboard, and then before displaying the solution Invalid Floating point operation . As I understand it, the error is a floating point, and what is more specific it is. Explain, please.

  #include <conio.h> #include <stdio.h> #include <vcl.h> #include <iostream> #pragma hdrstop char bufCur[256]; char* Cur(const char* text) { CharToOem(text, bufCur); return bufCur; } float vizn(float arr[][3]) { float vizn,p1,p2,p3; p1=arr[0][0]*arr[1][1]*arr[2][2]+arr[2][0]*arr[0][1]*arr[1][2]; p2=arr[0][2]*arr[1][0]*arr[2][1]-arr[2][0]*arr[1][1]*arr[0][2]; p3=-arr[0][0]*arr[2][1]*arr[1][2]-arr[1][0]*arr[0][1]*arr[2][2]; vizn=p1+p2+p3; return vizn; } #pragma argsused int main(int argc, char* argv[]) { float A[3][3]; int i, j; float B[3],V_A,x1,x2,x3,V_m_1,V_m_2,V_m_3,m_1[3][3],m_2[3][3],m_3[3][3]; V_A=vizn(A); printf(Cur("Введiть даннi в розширену матрицю:\n")); for (i=0;i<3;i++) { for(j=0;j<3;j++) { printf("A[%d][%d]=",i,j); scanf("%f",&A[i][j]); } } for (i=0;i<3;i++) { printf("B[%d]=",i); scanf("%f",&B[i]); } clrscr(); printf(Cur("\nРозширена матриця\n\n")); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf ("%.2f\t",A[i][j]); } printf("%.2f",B[i]); printf("\n"); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { m_1[i][j]=A[i][j]; m_2[i][j]=A[i][j]; m_3[i][j]=A[i][j]; } } for(i=0;i<3;i++) { m_1[i][0]=B[i]; m_2[i][1]=B[i]; m_3[i][2]=B[i]; } V_m_1=vizn(m_1); V_m_2=vizn(m_2); V_m_3=vizn(m_3); x1=V_m_1/V_A;////////////////здесь выбивает x2=V_m_2/V_A; x3=V_m_3/V_A; printf(Cur("\nРозв'язок")); printf("\n\nx1=%.2f\nx2=%.2f\nx3=%.2f",x1,x2,x3); getch(); return 0; } 

    1 answer 1

    Hmm ... The program takes a completely uninitialized matrix A , i.e. a matrix filled with meaningless unpredictable "garbage", and calculates its determinant in the string

     V_A=vizn(A); 

    As a result, the value of V_A also meaningless unpredictable garbage. And this is still “lucky” for you (or, rather, unlucky), because the program could have already crashed here.

    Only after that the program suddenly begins to ask the user for meaningful values ​​for the elements of the matrix A

    It is not surprising that later, lower in code, an attempt to divide by some unpredictable meaningless V_A value causes the program to fall from Invalid Floating point operation.

    Why are you trying to calculate the determinant A even before meaningful values ​​are entered into A ? From the point of view of banal common sense, it should be clear that you first need to fill the matrix A meaningful values, and only then try to calculate its determinant.

    • Exactly, then I just need to write V_A = vizn (A); below the input code of the array elements? - craftyperson
    • @craftyperson: Absolutely. This does not guarantee that your program will work (there may be errors), but this is the first thing that catches your eye. - AnT
    • fulfilled, but in the end that thing ( - craftyperson
    • @craftyperson: Look for errors further. I already see that your vizn determiner function is vizn written correctly. Those. it simply does not correspond to the usual 3x3 determinant formula from any textbook. Where did you get these formulas from? - AnT
    • you need to write the product of the main diagonal on triangle, the bases of which are parallel to it and take away the product of the side and triangles, the bases of which are parallel to it? ok - craftyperson