To make it straight forward: one can see there are two function declaration below.
void tableau_initialize(int Rn, int slack_num, int Cn, int A[Rn][Cn], int B[Rn], int C[Cn+1], float ***A_tableau, float **obj_array);
void array_debug(int row, int col, float *A);
Putting these two functions together into one program as follows.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
void tableau_initialize(int Rn, int slack_num, int Cn, int A[Rn][Cn], int B[Rn], int C[Cn+1], float ***A_tableau, float **obj_array);
void array_debug(int row, int col, float *A);
int max_ele(int* dynamic_array, int size);
int main()
{
int A[5][6] = {
{10, 11, 12, 0, 0, 0},
{0, 0, 0, 14, 12, 13},
{1, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 1}
};
int B[5] = {0, 0, 1, 1, 1};
int C[7] = {0, 0, 0, 0, 0, 0, 1};
float **Z;
float *optimal;
tableau_initialize(5, 2, 6, A, B, C, &Z, &optimal);
array_debug(1, 6, optimal);
}
void tableau_initialize(int Rn, int slack_num, int Cn, int
A[Rn][Cn], int B[Rn], int C[Cn+1], float ***A_tableau, float
**obj_array)
{
//artificial and objective variable numbers:
int art_num = Rn - slack_num;
int obj_num = 1;
//Non-standard variable number:
int non_std_var_num = Rn + obj_num;
//Total variable number:
int var_num = Cn+non_std_var_num;
//These arrays are respectively tableau array and results array.
//Tableau array structure is: [standard variables, slack variables, artificial variables, objective variables.]
//Allocate arrays for A_tableau and obj_array.
*A_tableau = malloc(sizeof(float *) * non_std_var_num);
for(int i = 0; i < non_std_var_num; i++)
{
(*A_tableau)[i] = malloc(sizeof(float) * var_num);
}
obj_array = malloc(sizeof(float) * non_std_var_num);
//This result contains results from constraints and objective function.
float B_full[non_std_var_num];
//Big M computations: with big M methods, our aim is to minimize
//Z = X - M*artifical variables;
//equivalently, we want to maximize -X + M*artificial
//variables.
float M = 1000000.0 * max_ele(&A[0][0], Rn*Cn);
//Fill the Tableau array:
for(int i = 0; i < Rn; i++)
{
//Array copy:
for(int j = 0; j < Cn; j++)
{
(*A_tableau)[i][j] = (float)A[i][j];
}
//Non-standard variable addition:
for(int j = 0; j < non_std_var_num; j++)
{
int result;
//Reverse the objective variable add it
if((j == Rn) && (i < slack_num))
{
result = -C[Cn];
}
else
{
//This is adding slack and artificial variable coefficient;
result = (i == j) ? 1 : 0;
}
(*A_tableau)[i][Cn+j] = (float)result;
}
}
//This is the last row within Tableau, this row is very
//special as it is the row of objective function.
for(int i = 0; i < var_num; i++)
{
//This is the standard and slack variable coefficient:
if(i < Cn+slack_num)
{
(*A_tableau)[Rn][i] = 0;
}
else
{
//This is the coefficient of obj variable.
if(i == var_num - 1)
{
(*A_tableau)[Rn][i] = -(float)C[Cn];
}
else
{
(*A_tableau)[Rn][i] = -M;
}
}
}
//Create 1-D dynamic array:
*obj_array = malloc(sizeof(float) * 6);
//Result array copy:
for(int i = 0; i < Rn; i++)
{
(*obj_array)[i] = (float)B[i];
}
(*obj_array)[Rn] = 0;
array_debug(1, 6, *obj_array);
}
void array_debug(int row, int col, float *A) {
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
printf("%f\t", *(A+i*col+j));
}
printf("\n");
}
printf("\n");
}
int max_ele(int* dynamic_array, int size)
{
if(!dynamic_array)
{
return 0;
}
int max_ele = dynamic_array[0];
for(int i = 1; i < size; i++)
{
if(max_ele < dynamic_array[i])
{
max_ele = dynamic_array[i];
}
}
return max_ele;
}
The output from the terminal screen is as follows:
0.000000 0.000000 1.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Why is array_debug printed in main() function showing that the dynamic array *optimal is filled with zeros, but in tableau_initialize(), it indicates that *optimal is a 1-D array with non-zero values? My expectation is I have two outputs like this:
0.000000 0.000000 1.000000 1.000000 1.000000 0.000000
0.000000 0.000000 1.000000 1.000000 1.000000 0.000000
tableau_initialize? How to create a Minimal, Reproducible Example