Implement the randomized incremental algorithm to compute the maximum of a linear objective
<p>Implement the randomized incremental algorithm to compute the maximum of a linear
objective function of four variables under a set of linear inequalities.
You should write a function</p>

<pre><code>◦ int rand lp(int n, double *A, double *b, double *c,
double *result)
</code></pre>

<p>which has as parameters the number of inequalities n, the coefficient matrix A and
right-hand side b, the coefficients of the objective function c, as well as the result
vector result, which contains the optimum values for the four variables x0, . . . , x3.
It returns an integer, which is the number of recomputations at the top level taken
by the algorithm to reac<code>z</code>h the optimum.</p>

<pre><code>Your function should solve the LP problem
max c[0]x0 + c[1]x1 + c[2]x2 + c[3]x3
A[0][0]x0 + · · · + A[0][3]x3 ≤ b[0]
A[1][0]x0 + · · · + A[1][3]x3 ≤ b[1]
...
A[n − 1][0]x0 + · · · + A[n − 1][3]x3 ≤ b[n − 1]
x0 ≥ 0, x1 ≥ 0, · · · , x3 ≥ 0
</code></pre>

<p>The matrix is a 4 by n matrix with n fairly large, so do not make any assumptions
on the size of the matrix; any additional storage you need should be allocated dynam-
ically.</p>

<pre><code>  #include &lt;stdio.h&gt;
    #include &lt;math.h&gt;
    #include &lt;iostream&gt;



using namespace std;

#define CMAX  200  //max. number of variables in economic function
#define VMAX  6  //max. number of constraints




//***********************************************
//Function prototypes


void Data();
void Pivot();
void Simplex() ;
void Results();
void Formula();
void Optimize();
int randp(int n, double **a, double *b, double *c,double *result);




//***********************************************
//Globals


int NC, NV, NOPTIMAL,P1,P2,XERR;
double TS[CMAX][VMAX];



//***********************************************
//Main Function



int main()  
{


  double **a , *b , *c , *res;
  int n,max_v;
  NV=10;
  TS[1][1]=0;


  while(NV&gt;4)
  {

      cout&lt;&lt;"ENTER THE NUMBER OF VARIABLES : ";

      cin&gt;&gt;NV;
      if(NV&gt;4) cout&lt;&lt;"ENTER VARIABLES LESS THAN 4"&lt;&lt;endl;
  }

  cout&lt;&lt;"ENTER THE NUMBER OF CONSTRAINTS : ";
  cin&gt;&gt;n;


  NC=n;

  max_v= randp(n,a,b,c,res);        //main function to compute maximum


    return 0;

}



//***********************************************
//RANDOM_LP


int randp(int n, double **a, double *b, double *c,double *result)
{


    a = new double*[n];
    b = new double[n];
    c = new double[NV];
    result = new double[NV];

    int i,j;



    for(i=0;i&lt;n;i++)

    {

        *(a+i) = new double[NV];

    }

    cout&lt;&lt;"ENTER COEFFICIENTS OF OBJECTIVE FUNCTION :"&lt;&lt;endl;
    for( i=0 ; i&lt;NV ; i++)
    {

        cout&lt;&lt;" #"&lt;&lt;i+1&lt;&lt;" :";
        cin&gt;&gt;*(c+i);


    }


    cout&lt;&lt;"enter the coficients of constraints :"&lt;&lt;endl;
    for( i=0 ; i&lt;n ; i++)
    {


        cout&lt;&lt;"Constraint#"&lt;&lt;(i+1)&lt;&lt;endl;

        for( j=0 ; j&lt;NV ; j++)
        {


            cout&lt;&lt;"#"&lt;&lt;(j+1)&lt;&lt;"   ";


            cin&gt;&gt;*(*(a+i)+j);

        }
        cout&lt;&lt;"right hand side = ";
        cin&gt;&gt;*(b+i);

    }






    for(i=2;i&lt;=(n+1);i++)
    {
        TS[i][1] = *(b+(i-2));



    }

    for(i=2;i&lt;=(n+1);i++)
    {
        for( j=2;j&lt;=NV+1;j++)
        {

            TS[i][j] = -*(*(a+i-2)+(j-2));


        }

    }

    for(i=2;i&lt;=(NV+1);i++)
    {
        TS[1][i] = *(c+i-2);



    }



    Data();
    Simplex();
    Results();

  for(i=0;i&lt;NV;i++)
  {

    *(result+i) = TS[2+i][1];

  }

  int max= TS[1][1];


    return max;


}

//***********************************************
//Function to put data into one matrix


void Data() 


{

    cout&lt;&lt;"RESULTS: "&lt;&lt;endl&lt;&lt;endl;

    for(int j=2; j&lt;=(NV+1); j++)  
    {
        TS[0][j] = j-1;
    }

    for( int i=NV+1; i&lt;=NV+NC; i++)  
    {
        TS[i-NV+1][0] = i;
    }

}



//***********************************************
//Function for computations


void Simplex() 
{


e10: Pivot();
     Formula();
     Optimize();
     if (NOPTIMAL == 1) goto e10;

}





//***********************************************
//


void Pivot() 

{

    double RAP,V,XMAX;

    XMAX = 0.0;

    for(int j=2; j&lt;=NV+1; j++) 
    {
        if (TS[1][j] &gt; 0.0 &amp;&amp; TS[1][j] &gt; XMAX) 
        {

            XMAX = TS[1][j];
            P2 = j;
        }

    }



    RAP = 999999.0;


    for (int i=2; i&lt;=NC+1; i++) 
    {

        if (TS[i][P2] &gt;= 0.0) goto e10;

        V = fabs(TS[i][1] / TS[i][P2]);

        if (V &lt; RAP) 
        {
            RAP = V;
            P1 = i;

        }

e10:;}


    V = TS[0][P2]; TS[0][P2] = TS[P1][0]; TS[P1][0] = V;

}




//***********************************************
// 

void Formula() 
{

    int i ,j;

    for (i=1; i&lt;=NC+1; i++) 
    {

        if (i == P1) goto e70;

        for (j=1; j&lt;=NV+1; j++) 
        {

            if (j == P2) goto e60;

            TS[i][j] -= TS[P1][j] * TS[i][P2] / TS[P1][P2];
e60:;
        }

e70:;
    }




    TS[P1][P2] = 1.0 / TS[P1][P2];

    for (j=1; j&lt;=NV+1; j++) 
    {

        if (j == P2) goto e100;

        TS[P1][j] *= fabs(TS[P1][P2]);

e100:;

    }


    for (i=1; i&lt;=NC+1; i++) 
    {

        if (i == P1) goto e110;

        TS[i][P2] *= TS[P1][P2];

e110:;

    }

}   




//***********************************************
//


void Optimize() 
{

    for (int i=2; i&lt;=NC+1; i++)
      if (TS[i][1] &lt; 0.0)  XERR = 1;

      NOPTIMAL = 0;

      if (XERR == 1)  return;

      for (int j=2; j&lt;=NV+1; j++)
      if (TS[1][j] &gt; 0.0)  NOPTIMAL = 1;

}




//***********************************************
//Function to show results


void Results() 
{

    int i,j;


    if (XERR == 0) goto e30;
    cout&lt;&lt;" NO SOLUTION "&lt;&lt;endl;        goto e100;


e30:
    for (i=1; i&lt;=NV; i++)
    {

        for (j=2; j&lt;=NC+1; j++) 
        {

            if (TS[j][0] != 1.0*i) goto e70;

            cout&lt;&lt;"       VARIABLE #"&lt;&lt;i&lt;&lt;" = "&lt;&lt;  TS[j][1]&lt;&lt;endl;

e70:  ;
        }
    }

    cout&lt;&lt;"       ECONOMIC FUNCTION: "&lt;&lt; TS[1][1]&lt;&lt;endl;
e100:;

}
</code></pre>
