954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove Zeros in an Array or Matrice

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
sota
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

If one matrice array is :

array ^ matrice_mixed_nums = gcnew array(5,7);

what should be the second's size

array ^ matrice_without_zero_lines = gcnew array(?,?);


Ps. matrice_mixed_nums are filled with random nums

sota
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

I'm not familiar with the syntax array^ (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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

If one matrice array is :

array ^ matrice_mixed_nums = gcnew array(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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Did you fix it?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thank you thines01

sota
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You