Printing sparse matrix

Hello all, currently working on a project which requires me to print a grid of numbers. The data that will be stored is in 2 separate variables of rows and columns within a struct used in a class array object. Whats is the best way to output the info so that the result will be a sparse matrix using the contents of both variables and will replace non elements with 0s.


layout of class info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  struct: oneItem
{
int row     // not arrays
int column // not arrays
}

class myClass:
{
 public:
   :
   :

private:
 oneItem matrix [10] ;

}
Last edited on
shouldn't oneItem have a value field?

youll want to sort your array of oneItems by rows then by columns.

then you can just do something like

int itemdx = 0;

for(r ... all the rows loop)
for(c.. all the cols loop)
{
if(itemdx < maxitemdx && matrix[itemdx].row == r && matrix[itemdx].column == c)
{
print value and increment itemdx
}
else print 0
}


Topic archived. No new replies allowed.