Click here to Skip to main content
15,748,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I execute the code, the cout does not run in the solve function.
Please tell me what the problem is?!

What I have tried:

#include <iostream>
#include <cmath>
#define size  20

using namespace std;


typedef float **Matrix;
typedef float *ptr;
typedef float  Vector[size];
/////////////////////////////

Matrix input (int );
void input_Info(int ,Vector);
void solve(int ,Matrix,Vector,Vector);
float determinant (Matrix mat ,int n);
Matrix getCofactor(Matrix, Matrix, int p,int q, int n);
////////////////////////////////////////////
int main()
{
     //float Information_matrix[n][1];
    int n;
    float **a;
    Vector Information_matrix,s;
   
    cout<<"---------Welcome----------- "<<endl;
    cout<<"How many unknowns can I solve? "<<endl;
    cin>>n;
    
    a=input(n);
    
    cout<<"Enter the equation information : ";
    input_Info(n,Information_matrix);
    
    cout<<"Solve the equation "<<endl;
    solve(n,a,Information_matrix,s);
    
}
//////////////////////////////////////////////////////
inline Matrix input(int n)
{
     float **Coefficient_matrix=new ptr[n];
     cout<<"Please enter the coefficients : "<<endl;
     
    for(int i=0 ; i<n ; i++)
    {
        Coefficient_matrix[i]=new float[n];
         for(int j=0; j<n  ; j++)
       cin>>Coefficient_matrix[i][j];
    }
     return Coefficient_matrix;
}
///////////////////////////////////////////////////////
void input_Info(int n,Vector Information_matrix)
{
    for(int i=0;i<n;i++)
      cin>>Information_matrix[i];
}
////////////////////////////////////////////////////////

inline  Matrix getCofactor(Matrix mat, Matrix temp, int p,
                 int q, int n)
{
    int i = 0, j = 0;
 
    // Looping for each element of the matrix
    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col < n; col++)
        {
            //  Copying into temporary matrix only those
            //  element which are not in given row and
            //  column
            if (row != p && col != q)
            {
                temp[i][j++] = mat[row][col];
 
                // Row is filled, so increase row index and
                // reset col index
                if (j == n - 1)
                {
                    j = 0;
                    i++;
                }
            }
        }
    }
}
////////////////////////////////////////////////////////
float determinant (Matrix mat ,int n)
{
     float D = 0; // Initialize result
 
    //  Base case : if matrix contains single element
    if (n == 1)
        return **mat;
 
    Matrix temp; // To store cofactors
 
    int sign = 1; // To store sign multiplier
 
    // Iterate for each element of first row
    for (int f = 0; f < n; f++)
    {
        // Getting Cofactor of mat[0][f]
        getCofactor(mat, temp, 0, f, n);
        D += sign * mat[0][f] * determinant(temp, n - 1);
 
        // terms are to be added with alternate sign
        sign = -sign;
    }
 
    return D;
}


////////////////////////////////////////////////////////
void solve(int n,Matrix a,Vector Information_matrix,Vector s)
{
   
    float detCoefficient_matrix = determinant(a,n);
     cout<< detCoefficient_matrix;
}
Posted
Updated 19-Jun-21 10:00am

getCofactor is defined as returning Matrix, but does not return anything. getCofactor should probably be declared as returning void

More importantly, in getCofactor, the parameter temp is not initialized. It is declared in determinant, but not initialized, and then does not have any memory allocated to it inside getCofactor, so attempts to assign a value to temp[i] cause a segmentation violation, which is the OS telling you that you've tried to access memory that the program does not own.
 
Share this answer
 
Use the debugger to find out what is going on. Read Set breakpoint in debugger to learn that basic skill in software development.
 
Share this answer
 
Quote:
Please tell me what the problem is?!

Use the debugger and discover by yourself, secondary effect it will improve your learning curve.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900