Hi mates,

I am using visual c++ . I need to remove those lines which are full of zeros.

Here is the matrice[5,7] :

2 2 9 8 7 3 4
    0 0 0 0 0 0 0
    3 4 5 7 8 9 6
    0 0 0 0 0 0 0
    1 3 4 5 5 5 5

Recommended Answers

All 9 Replies

Iterate through the rows checking which columns are zeros. Build a new array of rows that are not all zeros while you are doing this.

If one matrice array is :

array<int,2> ^ matrice_mixed_nums = gcnew array<int,2>(5,7);

what should be the second's size

array<int,2> ^ matrice_without_zero_lines = gcnew array<int,2>(?,?);


Ps. matrice_mixed_nums are filled with random nums

I'm not familiar with the syntax array<int, 2>^ (I suspect it is from C#), but I assume that syntax declares a structure similar to a 2 dimensional STL vector. If that's true then I would assume that the functionality is reasonably the same. If that's true, then I would check to see if there is an erase() method for the array<>^ class. If so, then you might not need to create a second array<>^ object. If not I would see if there is an alternate declaration for array<>^ that doesn't require discrete size (5, 7) at declaration. If so, then you wouldn't need to know what size to create the new array<>^ would be. If there is no erase() and you can't array<>^ doesn't expand automatically, then you will need to know the size of the new array<>^ object on declaration. In the above example it certainly looks like (3, 7) should work. If you know in advance what the new size will be, then go ahead and hard code the new size. Otherwise you should be able to keep track of the number of zeroed rows you encounter as you go through the original array<>^ object and then you can calculate the size of the new object and enter it on the fly. However, given the "advanced" "high level" functionality I would expect with syntax of array<>^ I would hope that you the appropriate functionality was built in so you wouldn't have to do it by hand, like you would in C using the basic C style array.

That's C++ that runs on the dot net CLR (managed C++).

If one matrice array is :

array<int,2> ^ matrice_mixed_nums = gcnew array<int,2>(5,7);

what should be the second's size

Count the rows that are not all zeros...

Ps. matrice_mixed_nums are filled with random nums

Then it's extremely probable that there are no rows with all zeros.

Then it's extremely probable that there are no rows with all zeros.

Adding to what WaltP said:
Once you run into your first non-zero on a given row, you have found a row that can be copied.

Did you fix it?

Since I saw you started another thread to try to solve this with vectors, I though I might intervene.

I could probably make this a little tighter, but if you have a function that takes a
2d array and returns an int of lines that are not ALL zero values, you can determine how big your new 2d array will need to be:

#include "stdafx.h"
using namespace System;
int GetNumGoodLines(array<int,2>^ arr_int2d)
{
   int intRetVal = 0;
   int iHeight = arr_int2d->GetUpperBound(0);
   int iWidth = arr_int2d->GetUpperBound(1);

   for (int i = 0; i <= iHeight; i++)
   {
      bool blnAllZeroes = true;

      for (int j = 0; j <= iWidth; j++)
      {
         int ix = arr_int2d[i, j];
         if (!ix.Equals(0))
         {
            blnAllZeroes = false;
            break;
         }
      }

      if (!blnAllZeroes)
      {
         intRetVal++;
      }
   }

   return intRetVal;
}

So if you have the array populated, you can detect the number of good lines by calling the function once.

int main(array<System::String ^> ^args)
{
   array<int,2>^ matrice_mixed_nums = gcnew array<int,2>(5,7)
   {
      {2,2,9,8,7,3,4},
      {0,0,0,0,0,0,0},
      {3,4,5,7,8,9,6},
      {0,0,0,0,0,0,0},
      {1,3,4,5,5,5,5}
   };
   //
   int intNumGoodLines = GetNumGoodLines(matrice_mixed_nums);
   Console::WriteLine("There are {0} rows to copy", intNumGoodLines);
   //
   if(intNumGoodLines.Equals(0))
   {
      return -1;
   }
//... more code goes here for the copy

Thank you thines01

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.