如何在内部使用char元素加倍2D数组的大小

时间:2014-02-04 08:20:21

标签: c

所以我想制作一个像这样的2D数组:
XX??XX??
XX??XX??
??XX??XX
??XX??XX
XX??XX??
XX??XX??

进入这个:
XXXX????XXXX????
XXXX????XXXX????
XXXX????XXXX????
XXXX????XXXX????
????XXXX????XXXX
????XXXX????XXXX
????XXXX????XXXX
????XXXX????XXXX
XXXX????XXXX????
XXXX????XXXX????
XXXX????XXXX????
XXXX????XXXX????

这是我到目前为止所做的:

void DoubleUP(char Box1[6][8], char Box2[12][16]){
int i,j,r,c;
r=0;
c=0;


for(i=0;i<6;i++){
    for(j=0;j<8;j++){

        if(Box1[i][j]== 'X'){
            Box2[r][c]='X';
            Box2[r][c+1] ='X';
            Box2[r+1][c] ='X';
            Box2[r+1][c+1] ='X';
            c+=2;
        }
        else{
            Box2[r][c]='?';
            Box2[r][c+1] ='?';
            Box2[r+1][c] ='?';
            Box2[r+1][c+1] ='?';
            c+=2;
        }


    }
    r+=2;

}   

任何帮助将不胜感激。或者,如果有一种更简单的方法可以让Box1以两倍的比例进入Box2,那也会有所帮助。感谢。

3 个答案:

答案 0 :(得分:8)

对于循环:

for (i = 0; i < 12; i++) {
    for (j = 0; j < 16; j++) {
        Box2[i][j] = Box1[i / 2][j / 2];
    }
}

答案 1 :(得分:1)

for(i=0;i<6;i++){
    for(j=0;j<8;j++){
        Box2[2*i][2*j]=Box1[i][j];
        Box2[2*i][(2*j)+1]=Box1[i][j];
        Box2[(2*i)+1][2*j]=Box1[i][j];
        Box2[(2*i)+1][(2*j)+1]=Box1[i][j];
    }
}

答案 2 :(得分:0)

您可以稍微缩短和概括代码,但是您对如何做到这一点有正确的想法。

void DoubleUP(char Box1[6][8], char Box2[12][16]){
int i,j,r,c;

    for (i=0, r=0; i<6; i++, r+=2) {
        for (j=0, c=0; j<8; j++, c+=2) {

            Box2[r][c]   = Box1[i][j];
            Box2[r][c+1] = Box1[i][j];
            Box2[r+1][c]  =Box1[i][j];
            Box2[r+1][c+1]=Box1[i][j];
        }
    }   
}