Monday, July 27, 2009

Any suggestions C programmers?

how do u compare an element from a simple array with an element in a 2D array and if they match, remove that element from the 2D array?

Any suggestions C programmers?
I'm not sure of what you mean when you say "remove that element," because that space is always there. There are a few ways that could be interpreted. For example, if the arrays are filled with non-zero integers, then putting a zero in that space could mean that the element was "removed." Or if they were arrays of string pointers, then putting in a NULL pointer could be a way of "removing" the element.





In any case, here is a simple example of what I would do. It takes two arrays of integers, iArray and i2DArray. I didn't put in the code to initialize the arrays with values; you'll have to do that.





int iArray[10];


int i2DArray[5][7];


int i, j, k;





/* fill in arrays */





/* remove items from i2DArray which match elements in iArray */


for (i = 0; i %26lt; 10; ++i)


{


   for (j = 0; j %26lt; 5; ++j)


      for (k = 0; k %26lt; 7; ++k)


      {


         if (iArray[i] == i2DArray[j][k])


            i2DArray[j][k] = 0;


      }


}

survey monkey

No comments:

Post a Comment