I was trying to code the assembly line sheduling problem using the dynamic programming approach. I have coded the program but i am facing problem with the 2-dimensional arrays I am using. For testing purpose, I wanted to initialize the array, but I was not able to get it. Moreover, as most of the arrays I am using are dynamically allocated, They were not getting passed to functions.
Though I have made the program working with some tricks but I would really appreciate if someone helps me improve it.
#include<iostream>
using namespace std;
//Had to make much of the variables global so
//as to sought out the problem of functions.
int n=6,i;
int a[2][6],t[2][6-1],e[2],x[2];
int f1[6],f2[6],l[2][6],fe,le;
void prnt_statns(){//int f1[],int f2[],int l[][],int fe,int le,int n){
int i=le;
cout<<"line "<<i<<" station "<<n<<"\n";
for(int j=n-1;j>0;--j){
i=l[i-1][j];
cout<<"line "<<i<<" station "<<j<<"\n";
}
}
void fast_way(){//int a[][n],int t[][n],int e[],int x[],int n){
//int f1[n],f2[n],l[2][n],fe,le;
f1[0]=e[0]+a[0][0];
f2[0]=e[1]+a[1][0];
for(int j=1;j<n;++j){
if( (f1[j-1]+a[0][j]) <= (f2[j-1]+t[1][j-1]+a[0][j]) ){
f1[j]=f1[j-1]+a[0][j];
l[0][j]=1;
}
else{
f1[j]=f2[j-1]+t[1][j-1]+a[0][j];
l[0][j]=2;
}
if( (f2[j-1]+a[1][j]) <= (f1[j-1]+t[0][j-1]+a[1][j]) ){
f2[j]=f2[j-1]+a[1][j];
l[1][j]=2;
}
else{
f2[j]=f1[j-1]+t[0][j-1]+a[1][j];
l[1][j]=1;
}
}
if( (f1[n-1]+x[0]) <= (f2[n-1]+x[1]) ){
fe=f1[n-1]+x[0];
le=1;
}
else{
fe=f2[n-1]+x[1];
le=2;
}
prnt_statns();//f1,f2,l,fe,le,n);
}
int main(){
cout<<"This program gives the fastest way through a factory of n machines..\n";
cout<<"Enter number of machines.. ";
/*int n,i;
n=6;//cin>>n;
int a[2][n],t[2][n-1],e[2],x[2];
*/
cout<<"Enter the entry times..\n1. ";
e[0]=2;//cin>>e[0];
cout<<"2. ";
e[1]=4;//cin>>e[1];
cout<<"Enter the exit times..\n1. ";
x[0]=3;//cin>>x[0];
cout<<"2. ";
x[1]=2;//cin>>x[1];
cout<<"Enter the station times of row 1:\n";
//a[0][]={7,9,3,4,8,4};//
for(i=0;i<n;++i) cin>>a[0][i];
cout<<"Enter the station times of row 2:\n";
//a[1]={8,5,6,4,5,7};//
for(i=0;i<n;++i) cin>>a[1][i];
cout<<"Enter transaction times from row 1:\n";
//t[0]={2,3,1,3,4};//
for(i=0;i<n-1;++i) cin>>t[0][i];
cout<<"Enter transaction times from row 2:\n";
//t[1]={2,1,2,2,1};//
for(i=0;i<n-1;++i) cin>>t[0][i];
fast_way();//a,t,e,x,n);
return 0;
}
I am also attaching the source code file for easier view.