Integrating int array into char array

时间:2015-12-14 18:02:15

标签: c arrays

I've created an int array which scanf values.
Now I want to take those values and use them to create a char array.
Meaning, the int value represent the number of cells to fill in the char array; if intarray=3, 3 chararray index will be filled with '*' or 'void'.

For example, if intarray is 3 4 2 1 then chararray will be (void)(void)(void)****(void)(void)*.

I am coding in C and cannot use strings.

Example run:

Enter picture size:  
10  
Enter 10 encoded lines:  
0 10 -1  
10 0 -1  
0 1 1 6 1 1 -1  
0 2 1 4 1 2 -1  
0 3 1 2 1 3 -1  
0 4 2 4 -1  
0 3 1 2 1 3 -1  
0 2 1 4 1 2 -1  
0 1 1 6 1 1 -1  
10 0 -1  

Output:

+----------+
|          |
|**********|
| *      * |
|  *    *  |
|   *  *   
|
|    **    |
|   *  *   |
|  *    *  |
| *      * |
|**********|
+----------+

This picture contains 34 asterisks.

My code so far:

int main()
{
int a[10][10]=ImageWithSmartClientData;
char b[10][10]=Screenshot;
int line=0, columna=0, columnb=0, temp1=0, temp2=0, size, count=0;
    printf("Enter picture size:\n");
    scanf("%d", &size);
    printf("Enter %d encoded lines:\n", size);
    for (line=0; line<size; line++){
        scanf(" %d", &temp1);
        while (temp1!=-1){
        a[line][columna]=temp1;
        columna++;
        scanf(" %d", &temp1);
        }
        columna=0;
    }
    printf("\n");
    for (line=0; line<size; line++){
        for (columna=0; columna<10; columna++){
        printf("%d ", a[line][columna]);
        }
        printf("\n");
    }
    columna=0;
    for (line=0; line<size; line++){
        while (a[line][columna]!=-1){
            a[line][columna]=temp2;
            while (temp2!=0){
                if (columna%2==0){
                b[line][columnb]='*';
                columnb++;
                temp2-=1;
                }
                else{
                b[line][columnb]=' ';
                columnb++;
                temp2-=1;
                }
                columna++;
                a[line][columna]=temp2;
            }
        }
    }
    for (line=0; line<size; line++){
        for (columnb=0; columnb<size; columnb++){
        printf("%c", b[line][columnb]);
        }
        printf("\n");
    }
printf("This picture contains %d asterisks", count);
return 0;
}

1 个答案:

答案 0 :(得分:0)

  

我的代码似乎卡住了

这是因为首先while (a[line][columna]!=-1)循环是无穷无尽的,因为linecolumna都不是a[0][0]不等于-1的条件变化。

考虑这个更简单的代码来填充b矩阵:

    for (line=0; line<size; line++)
        for (columnb=columna=0; columna<10; columna++)
            // let's use your temp2 for the end position up to which to fill b
            for (temp2 = columnb+a[line][columna]; columnb < temp2; columnb++)
                b[line][columnb] = "* "[columna%2];