-1

I have a following function, where I have to extract the matrix c and pass it as an argument instead of initializing it in the function body :

int ordonnancer (int n,  int n1, int m, vector <vector<int> > p, vector<int> ind){
  int Cmax;
  int c[m][n1];

  //intialize matrix "c" with 0
  for (int j=0;j<m;j++){ for(int i=0;i<n1;i++)  c[j][i]=0;  }

  //first task
  for (int j=0;j<m;j++){
    for(int k=0;k<=j;k++){
        c[j][ind[0]]=c[j][ind[0]]+p[k][ind[0]];
    }
  }

  //first machine 
  c[0][ind[0]]=0;
  for (int i=0;i<n;i++){
    for(int k=0;k<=i;k++){
        c[0][ind[i]]=c[0][ind[i]]+p[0][ind[k]];
    }
  }

  for(int i=1; i<n;i++){
    for(int j=1; j<m;j++){
        c[j][ind[i]]=max(c[j][ind[i-1]], c[j-1][ind[i]])+p[j][ind[i]];
    }
  }

  Cmax=c[m-1][ind[n-1]];

  return Cmax;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
user.mokho
  • 65
  • 5
  • 3
    Well, so what's your actual question? I can't spot one. – πάντα ῥεῖ Jan 13 '23 at 17:44
  • 4
    The array `c` is a *variable-length array*, and [C++ doesn't have them](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` instead. – Some programmer dude Jan 13 '23 at 17:44
  • 2
    As for your problem, you already seem to know how to pass vectors, so that should be rather easy to solve? Now you just have to use [a decent learning resource](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn about *references*. – Some programmer dude Jan 13 '23 at 17:45
  • Tip: Pass in your `std::vector` values as references, like `const std:vector>& p`. If you pass in by value you can make a lot of unnecessary copies. – tadman Jan 13 '23 at 18:10

1 Answers1

0

There are constructs that you cannot do in C++, and others, that you should not do.

C++ does not support VLA, Variable Length Arrays. An array like defined by you in `int c[m][n1];'. Both 'n1' and 'm' are variable. They are not compile time constants. But, this is a requirement in C++. So, it will not work.

Regarding passing arguments to a function. There are basically 3 methods:

  1. Pass by value. Like you do in your function. A copy will be made of all parameters that you hand in. And the outside world does not see any modification done to these variables in the function.
  2. Pass by pointer. Which is in essence also pass-by-value, but here the address of a variable will be passed (by value). And, if we dereference the address, we can access the addressed value and modify it. So, the modification will be visible outside the function. You will then see the '*'-symbol besides the parameter.
  3. Pass by reference. In your case, the best solution. A reference to the original variable will be passed to the function, and if you then modify the variable in the function, the referenced value (outside) the function will be updated. Additionally, no data will be copied. This save time and space. You will see the '&' beneath a variable.

So, you can define a vector<vector<int>> c outside your function. You can initialize it as you want and then pass it by reference to your function body.

This has the additional advantage, that the vector knows its size. And you do not need to hand in the parameters mand n1'. You can get the number of elements in a vectorby using itssize()'-function.

It looks a bit complicated to define and initialize a 2d vector. Therefore, I will show this to you using one of its constructors.

vector<vector<int>> c(m, std::vector<int>(n1, 0));

This will define a 2d vector with the needed number of elements and initialize everything to 0.

You can then define your function, for example, like this:

int ordonnancer(int n, vector<vector<int>>& p, vector<int>& ind, vector<vector<int>>& c) {

Please note that I pass all vectors per reference. See the '& symbol.

And in your function, you can get back the size of the 2d-vector for your loop with:

size_t m = c.size();

By the way, you should not use using namespace std;. Instead, you should use fully qualified names like std::vector.

Should you need further help, please comment

A M
  • 14,694
  • 5
  • 19
  • 44