thank bro you got the idea i was impleamenting
Quote:
Originally Posted by
Adak
I read his post a little differently. I thought he wanted to save just the rows that contained at least one value of 1, in them.
Code:
for(row = 0, j = 0; row < MaxRowsInOriginalArray; row++) {
goodrow = 0;
for(col = 0; col < MaxColsInOriginalArray; col++) {
if(OriginalArray[row][col] == 1)
goodrow = 1; //mark this row to be saved
break;
}
if(goodrow == 1) {
for each col - as above.
NoZeroArray[j++][col] = OriginalArray[row][col];
}
}
To work like this, the NoZeroArray must have the same number of columns as the OriginalArray has. It may need the same number of rows, IF the OriginalArray has no rows that are all zero's.
what is wrong with my code
it is print out junk values for some reason
Code:
#include <stdio.h>
int main(int argc,char *argv[]){
int row;
int col;
int goodrow=0;
int j;
int array[20][21]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,0},
{0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1},
{1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
{0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0},
{1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,0},
{0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
{0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0},
{0,0,1,1,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
{1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0},
{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
int resultarray[14][20]={};
for(row = 0, j = 0; row < 20; row++) {
goodrow = 0;
for(col = 0; col < 21; col++) {
if(array[row][col] == 1)
goodrow = 1; //mark this row to be saved
break;
}
if(goodrow == 1) {
for (col=0;col<21;col++) {
resultarray[j++][col] = array[row][col];
}
}
}
for(row=0;row<j;row++){
for(col=0;col<21;col++){
printf("%d",resultarray[row][col]);
}
printf("\n");
}
return 0;
}
cheer bro