C++ array questions

Hi. I'm a newbie in programming and I'm having difficulty on how I can apply an array on my project. I'm trying to create a 2 dimensional 4 by 4 array. The value of the array will be from user. What I'm at lost right now is that if there are any zeros in the third row, the corresponding column will not be included on the printing. So for example, if the value on the 3rd row are 0,0,2,3, all the values of column 0 and 1 will not be printed, only columns 2 and 3.
Last edited on
You need to check if the value if != 0 before you print it
1
2
3
4
5
6
7
8
9
10
11
int data[4][4] = 
{
   {1, 2, 3, 4},
   {1, 2, 3, 4},
   {0, 0, 2, 3},
   {1, 2, 3, 1}
};

for (int row = 0; row < 4; ++row)
  for (int col = 0; col < 4; ++col)
    // check if data[row][col] if is != 0. If yes print it 
Thank you for the response. I've actually did the same thing earlier but what I'm curious is if there is any way that I can print only the column 2 and 3 since there is a zero on row 3 column 0 and 1. Basically if there is any zero in row 3, totally eliminate the intersecting column and just print the column without any zero referencing row 3? In this case the printed output will be:
3 , 4
3, 4
2, 3
3, 1
I've been trying to check multidimensional array properties but I found nothing that can help.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int data[4][4] = 
{
   {1, 2, 3, 4},
   {1, 2, 3, 4},
   {0, 0, 2, 3},
   {1, 2, 3, 1}
};
bool ColPrint[4] = {false,false,false,false};

int row, col;

for (col = 0; col < 4; ++col)
{
   for (row = 0; row < 4; ++row)
   {
      if( data[row][col] == 0 )
         break;
   }
   
   if(row>=4)
      ColPrint[col] = true;
   else
      ColPrint[col] = false;
   
}

for (row = 0; row < 4; ++row)
{
   for (col = 0; col < 4; ++col)
   {
      if(ColPrint[col] == true)
         // print data[row][col] 
   }
}
Topic archived. No new replies allowed.