0

i'm struggling a bit to make my program work, it's a school homework we have to do for the holidays and basically this is what we have to do :

We are trying to fill a knapsack to it's maximum capacity while maximising it's value with objets from a given list.

i won't bore you with the details so i'll just go straight to where i'm currently having an issue.

i have a 2d dynamic array, i'm trying to check if a line of said array doesn't contain one of the elements in another array and if it does then i need to put everything on that line in a second array.

let's say i have this code :

    int **exc, rexc, *lexc, i=0;
    printf("\nHow many exclusion :");
    scanf("%d", &rexc);
    exc = malloc( sizeof(int *) * rexc);
    for(i=0;i<rexc;i++){
        printf("\nHow many elements in exclusion %d : ", i);
        scanf("%d", &lexc[i]);
        exc[i] = malloc( sizeof(Objet) * lexc[i]);
        printf("\n the elements are :");
        for(j=0;j<lexc[i];j++){         
            scanf("%d",&exc[i][j]);
    }
}

With this i have initialized and filled my array. Now i have another array with values let's say it's something like inter[1]={5,6} impos[1]={3,7}

And let's say our 2d array looks like this :

exc[3][3]={3,6,2}{2,7,1}{5,2,4}

What i need to do is search through my 2d array, if any line contain 3 or 7 ( it cannot contain both, i check that by using a function that make sure that there's no problem before running this function ), then every other elements of that line need to be added to the inter[] array.

How i thought i'd make this work was by doing something like this :

int count=0,i=0, j=0, k=0;
while(count<cimp){
 for(i=0;i<rexc;i++){
    for(j=0;j<lexc[i];j++){
    if (exc[i][j] = impos[count])
        for (k=0;k<lexc[i];k++){
        if(k!=j){

        inter[cinter+1]=exc[i][k];
        cinter++;
        }
    }
}
count++;
}

cinter/cimp is a variable where i stored the number of elements in inter/impos.

the result i'm getting is kinda funky tho here's an output :

How many exclusion : 3

How many elements in exclusion 1 : 3
the elements are : 3 6 2
How many elements in exclusion 2 : 3
the elements are : 2 7 1
How many elements in exclusion 3 : 3
the elements are : 5 2 4

How many obligatory elements : 1
Which are : 5

Forbidden list : 5 0 6 2 5 2 5 5 7 1 5 1 5 5 2 4 5 4 5

I'm still getting used to C, hope i could get any help from you guys and sorry for making it such a long post !

  • 1
    "i have a 2d dynamic array" - No you don't. A pointer is not an array, thus a pointer-to-pointer is not a 2D array. It also cannot point to one. – too honest for this site Apr 21 '17 at 14:30
  • @Olaf can you explain what you mean ? if you don't know the size of the array, since it's up to the user how many row there is and how many elements are in the lines, how else are you supposed to make an array if not by pointers ? And how else are you supposed to make a 2d array if not by ptr to ptr ? – Amine Chentouf Apr 21 '17 at 14:43
  • What is the problem? Just allocate a dynamic 2D array. – too honest for this site Apr 21 '17 at 14:46
  • @AmineChentouf See [Correctly allocating multi-dimensional arrays](http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Apr 21 '17 at 14:46
  • @AmineChentouf "how else are you supposed to make a 2d array" --> `int execA[rexr][rexc];` or a pointer to a 2D: `int (*execB)[rexr][rexc] = malloc(sizeof *execB);` – chux - Reinstate Monica Apr 21 '17 at 15:28

0 Answers0