in this piece of code i have a matrix and if a sum of a row is not even, i have to delete it, but how to delete a row in a dynamically allocated multi-dim array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void filterMat(int**& matrix, int cols, int& rows)
{
int sumRows = 0;
for (size_t i = 0; i < rows; i++)
{
sumRows = 0;
for (size_t j = 0; j < cols; j++)
{
sumRows += matrix[i][j];
}
if (sumRows % 2 != 0)
{
delete[]matrix[i];
matrix[i] = nullptr;
}
}
}
this throws an exception so its no corect but i cound think of any other way to delete it
also should do this task without extra memory /without creating a new dyn alloc multi-dimensional array/
Until I have a chance to set something up, which may not match the code that you have, I am thinking that in the "filterMat" function first you would have to delete all the columns before you delete the row. and instead of matrix[i] = nullptr;, because there would be no "matrix[i]" you would need rows--;.
You could also write if (sumRows % 2). As long as the returned value is something other than (0) it would be true.
Posting enough code to duplicate your problem if very helpful.
I was thinking something similar Dutch. If they're going to be moved anyways just overwrite the row being deleted and delete the end or just disregard it via a separate tally.