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.

You're taking avery "last-century-C-like" approach here. Although it is good to know about malloc alloc, arrays and stuff the STL is a much better choice in most cases.
since you already use the STL for your streams, you should also use vectors/maps/lists etc. Get used to them they'll save you a lot of grief.
If you want to pass arrays to functions you need to define the functions similar to

void f(int* v, int sizeV) // only change the values
{
    v[3] = 42;
}

or

void f(int*& v, int sizeV) // also change the pointer
{
    v=new int[100];
}

or a host of similar ways. Quite dangerous if you're not experienced.
much easier and more powerful are STL containers:

void f(vector<int>& v) // Mind the ampersand & for pass by reference
{
    /// v.size() gives you the number of elements in v
    /// v resize(500) resizes your vector...
    /// v[120] = 42; sets element at position 121 to 42 (0-based like arrays!!)
}

hope that kicks you off...

I am not using STL here because this program is to be converted to C. (after it starts functioning like I want it to). So, maybe you understand this "last century C-like approach". ;)
Anyways, I tried using your way of passing the pointer on a test program:

#include<iostream>
using namespace std;

void prnt(int * a,int i,int j){
    for(int k=0;k<i;++k)
    for(int l=0;l<j;++l)
    cout<<a[k][l];
    }

int main(){
    int a[3][4];
    for(int k=0;k<3;++k)
    for(int l=0;l<4;++l)
    a[k][l]=k*l;
    prnt(a,3,4);
	return 0;
	}

The errors include:

||In function `void prnt(int*, int, int)':|
7|error: invalid types `int[int]' for array subscript|
|In function `int main()':|
15|error: cannot convert `int (*)[4]' to `int*' for argument `1' to `void prnt(int*, int, int)'|
||=== Build finished: 2 errors, 0 warnings ===|

So, can you please be more clear with your approach on pointers as I have never been too comfortable with these dangerous pointers.. :(
Any help would seem good.

And yes, The methods you stated seem to be for arrays, but I am asking for multidimensional arrays. So, please try to stick to the area of concern...

>>The methods you stated seem to be for arrays, but I am asking for multidimensional arrays. So, please try to stick to the area of concern..
Once you figure out single-dimensional arrays, it is not a big leap to multi-dimensional, so it is "the area of concern". Each dimension of an array is one (1) level of pointer indirection. Which means that a 2-dimensional array has 2-levels of indirection whereas a 1-dimensional array only has one (1) level of indirection. All you need to do is expand the levels of indirection you take into account in your parameter declarations.

To pass an array, you need to either directly specify the parameter as an array:

void exampleFunction(int[], int);

int main() {
  const int size = 10;
  int myInts[size] = {0};

  exampleFunction(myInts, size);

  return 0;
}

or create it dynamically:

void exampleFunction (int *, int);

int main() {
  const int size = 10;
  int *pInt;

  pInt = new int[size];

  exampleFunction(pInt, size);

  return 0;
}

or pass the pointer:

void exampleFunction (int *, int);

int main() {
  const int size = 10;
  int myInts[size] = {0};

  exampleFunction(myInts, size);

  return 0;
}

Now, expand the dynamic version to multi-dimensional:

void exampleFunction (int **, int, int);

int main() {
  const int rows = 10;
  const int cols = 5;
  int **pInt;

  pInt = new int*[rows];
  for (int i = 0; i < rows; ++i) {
    pInt[i] = new int[cols];
  }

  exampleFunction(pInt, rows, cols);

  return 0;
}

These all demonstrate declaration and population. From this point, I think you should be able to figure out initialization.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.