I need to write a code that is gonna remove a whole row/column in a matrix(either static or dynamic matrix)

Recommended Answers

All 6 Replies

What do you mean by "matrix", what do you mean by "remove"?

For the first part I have to make a multidimensional array(matrix). When I make one (any size) I have to find longest subarray of positive numbers in one row and compare it with other rows(if I my row has more then one subarray of positive numbers I have to include the longest). When comparing other rows with elements of subarray of positive number if I find same numbers from subarray in that row I have to "remove" that row.
The other part is making a matrix with double pointer and doing the same thing with subarray and comparing rows.
If I wasn't so clear on what I have to do, please contact my via PM. :P

You can't just remove a row from a statically allocated array, and it's a little tricky to remove a row from a dynamically allocated array.

For statically allocated arrays the best you can do is to copy the contents of all the rows you don't want to remove up to fill in the gap, then the last row in the array is considered unused.

For example: if you have an array of 10 rows, in order to remove row 2 you have to copy rows 3-10 up to rows 2-9 then somehow mark row 10 as unused. The array still has 10 rows, but your program would consider the last row as unavailable.

For dynamic arrays when you want to remove row 2, do it in several steps
Step 1. malloc() a new array the same size as the original but with 1 fewer rows.
Step 2. copy all the rows from the original to the new, omitting the row to be deleted.
Step 3. free() the original array
Step 4. set the pointer of the original array to be that of the new array.

realloc() might work here but only if the row to be deleted is the last row in the array, so it's not useful in your program.

longest subarray of positive numbers in one row

I don't know what you mean by that. Does it mean the array can contain both positive and negative numbers? Post a simple example to illustrate the idea.

1 2 3 -5 6 2
2 3 -5 -6 -4 2
-2 2 5 1 7 -8
-5 -6 2 5 1 7

So it this 'matix' I need to find the longest array of positive numbers in one row(not column). In this case it's 2 5 1 7 and 'remove' it from row 3 and 4.

Oh, yea. Forgot to say that you can use real numbers in matix. xD

Forgot to say that you can use real numbers in matix.

That's ok -- you can't put irrational numbers in an int anyway.

Finding the largest array of positive numbers should be fairly straight forward. Just cound the number of positive numbers until either the end of the row is hit or a negative number is found. You will probably need to use two other arrays to do that.

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.