2013-07-18 55 views
0

我想将一个500x8的matrix的行(每个团队迭代一个)复制到名为actual_row的临时数组中。这是我试过的。将矩阵的每一行复制到一个临时数组

int matrix[500][8]; // this has been already filled by int's 
int actual_row[8]; 
for(int i = 0; i < 500; i++) { 
    for(int j = 0; j < 8; j++) { 
     actual_row[j] = matrix[i][j]; 
     printf("The row is: "); 
     for(int q = 0; q < 8; q++) { 
       printf(" %d ",actual_row[q]); 
     // do other stuff 
     } 
     } 
printf("\n"); 
} 

这不是打印行,它打印0和1的某个时间,所以有一些我做错了。
在此先感谢。

回答

1

你的逻辑稍微偏离。您需要将行复制到actual_row,然后打印内容。此外,为什么不直接打印的内容,但要复制的矩阵行actual_row

printf("The row is: "); 
for(int j = 0; j < 8; j++) { 
    actual_row[j] = matrix[i][j];   
    printf(" %d ",actual_row[j]); 
    // do other stuff 
} 

所以您的代码段应该是这样的:

int matrix[500][8]; // this has been already filled by int's 
int actual_row[8]; 
for(int i = 0; i < 500; i++) { 
    printf("The row is: "); 
    for(int j = 0; j < 8; j++) { 
     actual_row[j] = matrix[i][j];   
     printf(" %d ",actual_row[j]); 
     // do other stuff 
    } 
    // <--at this point, actual_row fully contains your row 
printf("\n"); 
} 
1

您的逻辑稍微关闭(不需要第三个嵌套循环)。您需要将相同的循环中的行复制到actual_row(你没有),并打印内容:

printf("The row is: "); 
for(int j = 0; j < 8; j++) { 
    actual_row[j] = matrix[i][j];   
    printf(" %d ",actual_row[j]); 
    // do other stuff 
} 
2

不打印actual_row之前它完全填满:

for(int j = 0; j < 8; j++) { 
    actual_row[j] = matrix[i][j]; 
} 

printf("The row is: "); 
for(int q = 0; q < 8; q++) { 
     printf(" %d ",actual_row[q]); 
     ... 
} 
相关问题