C++ c++;引用数组数组(指针到指针?)

C++ c++;引用数组数组(指针到指针?),c++,arrays,pointers,reference,C++,Arrays,Pointers,Reference,我正在为一门课程编写单纯形法,我在引用数组(矩阵)或指针数组(行话不确定)时遇到问题。我主要想做的是,当标准输入文件有东西要读时,我求解单纯形法。我从表格的txt文件中读取: m n b1 a1 a2。。。上午 : BNA a2。。。一个 在函数read_问题中,我将此文本文件读入矩阵a。只要文本文件中还有其他内容需要读取,此函数就会返回true。我可以将值读入矩阵,但当我返回到主矩阵时,矩阵A为空。这个函数没有做的(我希望它做的)是引用main中定义的矩阵,并且能够编辑read_问题中的值。我

我正在为一门课程编写单纯形法,我在引用数组(矩阵)或指针数组(行话不确定)时遇到问题。我主要想做的是,当标准输入文件有东西要读时,我求解单纯形法。我从表格的txt文件中读取:

m n

b1 a1 a2。。。上午

:

BNA a2。。。一个

在函数read_问题中,我将此文本文件读入矩阵a。只要文本文件中还有其他内容需要读取,此函数就会返回true。我可以将值读入矩阵,但当我返回到主矩阵时,矩阵A为空。这个函数没有做的(我希望它做的)是引用main中定义的矩阵,并且能够编辑read_问题中的值。我该怎么做

任何帮助都会很好

int solve_simplex_phase1(int n, int m, double **Aij,int * basis, int pivot_rule);
bool read_problem(int* n, int* m, double **A, int *basis);

int main( int argc, char *argv[] ){

    double **A; // global variable
    int *basis; // global variable      
    int pivot_rule; //pivot rule

    //      Pivot Rule: 
    //      1 - smallest subscript. 
    //      2 - largest coefficient 
    //      3 - maximum increase 
    if( argc == 2){
        if ( argv[1][0] == '1'){
            pivot_rule =1;
        }else if ( argv[1][0] == '2'){
            pivot_rule = 2;
        }else if ( argv[1][0] == '3'){
            pivot_rule = 3;
        }else{
            cout<< "Error - Incorrect Input Pivot Rule \n";
            return 0;
        }

    }else{
        cout<< "Error - No Input Pivot Rule \n";
        return 0;
    }


    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";

    int m,n;
    bool read_problem_tf;
    do{
        read_problem_tf=read_problem(&n , &m , A, basis);

            if(A==NULL ) { cout << "WHY!!!!!x\n" ;}
            if(basis==NULL ) { cout << "WHY22222!!!!!x\n" ; return 0;}
            // call simplex function to solve this problem
            solve_simplex_phase1( n , m , A, basis, pivot_rule);  

    }while(read_problem_tf);
    // while( read_problem(&n , &m , A, basis) ) {

    //      // cout << n << "\n";
    //      // cout << m << "\n";
    //      if(A==NULL ) { cout << "WHY!!!!!x\n" ;}
    //      if(basis==NULL ) { cout << "WHY22222!!!!!x\n" ; return 0;}
    //      // call simplex function to solve this problem
    //      solve_simplex_phase1( n , m , A, basis, pivot_rule);  

    // }
}


bool read_problem(int *nn, int *mm, double **A, int *basis) {
    //  PHASE 1 ARCHITECTURE
    //    0   1 2 ............. n n+1 ......... n+m  n+m+1
    //  ------------------------------------------------
    //  | 0 | objective function | 0 ........... 0 | 0 |  0
    //  ------------------------------------------------
    //  |   |                    |                 | 0 |  1
    //  |   |                    |                 |   |   
    //  |   |                    |                 | : |  :
    //  | b |          A         |        I        | : |  :
    //  |   |                    |                 | : |  :
    //  |   |                    |                 | : |  :
    //  |   |                    |                 |   |   
    //  |   |                    |                 | 0 |  m
    //  ------------------------------------------------
    //  | 0 | 0 .............. 0 | 0 ........... 0 | 0 | m+1
    //  ------------------------------------------------
    //

    bool result = true;
    int n;
    int m;

    if(scanf( "%d%d", nn, mm) != EOF){ // find size of problem
    n=*nn;
    m=*mm;

        basis = new int [n+m+2]; //create basis vector 0 if not in basis, 1 if in basis, 2 for constant value. 

        // define matrix A
        A = new double *[m+2]; // m+2 to take into account phase 2 problems


        for (int i = 0; i <= m+2 ; i++) {

            A[i] = new double [n+m+2];// n+m+2 to take into account phase 2 problems

        }

        // scan matrix for remaining entries
        for (int row = 0 ; row <= m ; row++) { // ROWS

            for (int col = 0 ; col <= n+m ; ++col) { // COLUMNS

                if (row == 0) {

                    if (col > 0 && col <= n) {
                        scanf( "%lf" , &A[row][col] );
                        // std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
                        basis[col]=0; // define basis

                    } else {
                        A[row][col] =0.00000;
                        // std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
                        basis[col]=1; // define basis
                    }

                }else if (row > 0) {

                    if ( col <= n) {
                        scanf( "%lf" , &A[row][col] );
                            if( col !=0){
                                A[row][col]*=(-1);  
                            }

                        // std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';


                    } else if (row == col - n ) {
                        A[row][col]=1.00000;
                        // std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';


                    } else {
                        A[row][col]=0.00000;
                        // std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';

                    }

                }

            }

        }

        // sneak dimension into basis (unused 0,0 spot)
        basis[0]=2;//m+n; // fix (0,0) of basis

    }else{
        result = false;
    }
    return result;
}   

int solve_simplex_phase1(int n, int m, double **Aij, int * basis, int pivot_rule) {
    // function called to solve the simplex method
        cout << "//=========================================================//\n";
        cout << "//            LP Solver - Simplex Method                   //\n";
    //      Pivot Rule: 
    //      1 - smallest subscript. 
    //      2 - largest coefficient 
    //      3 - maximum increase 


    // Take the input and transform the problem into standard form
    //      - assumed to be in standard form
        if(pivot_rule == 1){ 
            cout << "//            USING:  Smallest Subscript                   //\n\n" ;
        }else if(pivot_rule == 2){ 
            cout << "//            USING: Largest Coefficient                   //\n\n" ;
        }else if(pivot_rule == 3){ 
            cout << "//            USING: Maximum Increase                      //\n\n" ;
        }

    // Define Initial Dictionary
        cout << "\n Initial Dictionary: \n";

    // Define Objective function location (row)
        int objective_function_row = 0;
        display_dictionary(0 , 0 , 0 , n+m , m , Aij , basis, objective_function_row);

    //  If the linear program does not have an initial feasible origin 
    //  (that is , one of the bi's is strictly less than zero), 
    //  terminate;  

        if (!check_positive_objective_function( n+m , m , Aij, objective_function_row)){
            cout << "//=========================================================//\n\n";
            return 0;
        }

        // check boundedness of LP problem
        if (!check_boundedness(n+m , m , Aij , basis , objective_function_row)){
            cout << "\n ERROR - LP is not bounded \n\n";
            cout << "//=========================================================//\n";
            return 0;
        }

        // check for phase 1 or phase 2 problem
        if (!check_positive_bs(n+m , m , Aij)){

            cout << "\n Initial Dictionary Infeasible - Auxiliary Problem \n";

            solve_auxiliary( m+n+1, m+1, Aij, basis, pivot_rule);
            // cout << "exit auxiliary?" << flush;

            if(check_feasible_auxiliary(n+m+1,m+1,Aij,basis)){

                cout << "\n Auxiliary problem feasible, continue to phase 2\n" <<flush;

            } else if (!check_feasible_auxiliary(n+m+1,m+1,Aij,basis)){

                if(check_degenerate_pivot(n+m+1 , m+1 , Aij, basis)){

                    take_degenerate_pivot(m+n+1 , m+1 , Aij , basis);

                    if(check_feasible_auxiliary(n+m+1 , m+1 , Aij, basis)){

                        cout << "\n Auxiliary problem feasible after degenerate pivot, continue to phase 2\n" <<flush;

                    }else{

                        cout << "\n ERROR in Auxiliary - Infeasible \n\n"<<flush;
                        cout << "//=========================================================//\n";
                        return 0;
                    }

                } else {

                        cout << "\n ERROR in Auxiliary - Infeasible \n\n"<<flush;
                    cout << "//=========================================================//\n";
                    return 0;

                } 

            }

        }


    int iteration_count=1;
    do {
        /* Iterate dictionaries 
            1) find in and out variables
            2) pivot dictionary;
            4) run checks;
            5) go to 1) until z row has no positive c;
        */
                    // cout << "check z row: " << check_positive_objective_function( n+m , m , Aij)<<'\n';
                    // cout << "iteration" << iteration_count << '\n';

        // Entering variable
        int in_col=find_pivot_col( n+m , m , Aij , pivot_rule, basis, objective_function_row);
            // cout << "why?"<<objective_function_row<<endl<<flush;
        // leaving variable (this is the ROW of the leaving variable not the variable Label! )
        int in_out_row=find_pivot_row( in_col , m , Aij);

        // take the in_out_row and find the column of the output variable
        int out_col=find_leaving_variable(in_out_row,n+m,Aij,basis, objective_function_row);

        // perform pivot & updates basis
        pivot( in_out_row , in_col , out_col , n+m , m , Aij, basis);

        // display dictionary
        display_dictionary(iteration_count , in_col , out_col , n+m , m , Aij , basis, objective_function_row);

        // perform checks (b's are positive, bounding of columns)
        // check for phase 1 or phase 2 problem
        if (!check_positive_bs(n+m , m , Aij)){

            cout << "\n ERROR - LP does not have a feasible origin. \n\n"<<flush;
            cout << "//=========================================================//\n"<<flush;
            return 0;
        }

        // check boundedness of LP problem
        if (!check_boundedness(n+m , m , Aij , basis, objective_function_row)){
            cout << "\n ERROR - LP is not bounded \n\n"<<flush;
            cout << "//=========================================================//\n"<<flush;
            return 0;
        }

        // check if you are in an infinite loop
        if( iteration_count > 300){
            cout << "\n ERROR - Infinite Loop? Try another pivot rule\n" <<flush;
            cout << "//=========================================================//\n"<<flush;
            return 0;
        }

        iteration_count++;

    } while (check_positive_objective_function( n+m , m , Aij, objective_function_row));
        cout << "\n";
        cout << "//---------------------------------------------------------//\n\n";
        cout << "Objective function has all negative terms:" << '\n';

    // export solution
    return_optimal_solution(n+m, m, Aij, basis);

    cout << "//=========================================================//\n\n\n\n\n";

    return 0;
}
int solve\u simplex\u phase1(int n,int m,double**Aij,int*基,int pivot\u规则);
布尔读问题(整数*n,整数*m,双**A,整数*基);
int main(int argc,char*argv[]){
double**A;//全局变量
int*basis;//全局变量
int pivot_rule;//pivot rule
//轴心规则:
//1-最小下标。
//2-最大系数
//3-最高增幅
如果(argc==2){
如果(argv[1][0]=“1”){
pivot_规则=1;
}else if(argv[1][0]=“2”){
枢轴_规则=2;
}else if(argv[1][0]=“3”){
支点规则=3;
}否则{
cout将
读取问题()
签名更改为

bool read_problem(int* n, int* m, double **&A, int *basis);
                                         ^^^^