C 如何从数组中获取循环参数并将其更改为打印符号
我有过类似的经历。这个程序必须从用户那里提取5个单词,然后将它们的长度打印成“直方图”。问题是,我应该怎么做。长话短说,如何写一个循环,从单词长度数组的每个单元格中提取int,并精确打印出那么多的“#”符号:PC 如何从数组中获取循环参数并将其更改为打印符号,c,C,我有过类似的经历。这个程序必须从用户那里提取5个单词,然后将它们的长度打印成“直方图”。问题是,我应该怎么做。长话短说,如何写一个循环,从单词长度数组的每个单元格中提取int,并精确打印出那么多的“#”符号:P char word_input[20]; int counter = 0; int word_lenght[5]; while(counter < 5 ) { printf("Please, write a word of your choice: "); sca
char word_input[20];
int counter = 0;
int word_lenght[5];
while(counter < 5 )
{
printf("Please, write a word of your choice: ");
scanf("%20s", word_input);
word_lenght[counter] = strlen(word_input);
counter++;
}
printf("\n");
printf("Your histogram: \n");
for(int i = 0; i < counter; i++)
{
printf("%d.%d\n",i,word_lenght[i]);
}
char word_输入[20];
int计数器=0;
整数字长度[5];
while(计数器<5)
{
printf(“请,写一个你选择的词:”);
扫描频率(“%20s”,字输入);
字长[计数器]=strlen(字长输入);
计数器++;
}
printf(“\n”);
printf(“您的直方图:\n”);
对于(int i=0;i
还有,我想在这里问一下;我不想再写一篇文章。我正在写Prata的书(还有,学习git的同时把每一个练习都放在那里)。从C开始就足够了吗?我看到了这一点:,我想了解一下,至少是关于编程的一切:P
我向您提出了一个想法,当然不是最好的,但这是您思考它的一个开始
#define SIZE_ARRAY 5
int main(void)
{
int word_length[SIZE_ARRAY] = {4, 2, 6, 3, 7};
int max_value = 0;
for (int i = 0 ; i < SIZE_ARRAY ; ++i)
{
if (word_length[i] > max_value)
max_value = word_length[i];
}
for ( ; max_value > 0 ; max_value--)
{
for (int j = 0 ; j < SIZE_ARRAY ; j++)
{
if (word_length[j] >= max_value)
putchar('#');
else
putchar(' ');
}
putchar('\n');
}
return (0);
}
#定义数组5的大小
内部主(空)
{
int-word_-length[SIZE_-ARRAY]={4,2,6,3,7};
int max_值=0;
对于(int i=0;i最大值)
最大值=字长[i];
}
对于(;最大值>0;最大值--)
{
对于(int j=0;j=最大值)
putchar(“#”);
其他的
putchar(“”);
}
putchar('\n');
}
返回(0);
}
%20s
-->%19s