我创建了一个包含 scanf 值的 int 数组。
现在我想获取这些值并使用它们来创建一个 char 数组。
意思是,int值代表要填充char数组的单元格数量;
如果 intarray=3,则 3 个字符数组索引将用 '*' 或 'void' 填充。
例如,如果 intarray 为 3 4 2 1
则
chararray 将是 (void)(void)(void)****(void)(void)*
。
我正在使用 C 进行编码,无法使用字符串。
运行示例:
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
输出:
+----------+
| |
|**********|
| * * |
| * * |
| * *
|
| ** |
| * * |
| * * |
| * * |
|**********|
+----------+
这张图片包含 34 个星号。
到目前为止我的代码:
int main()
{
int a[10][10]={{0}};
char b[10][10]={{0}};
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;
}
最佳答案
my code seems to get stuck
这是因为 while (a[line][columna]!=-1)
循环是无限的,因为 line
和 columna 都没有
也不改变 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];
关于c - 将 int 数组整合到 char 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34273654/