函数“histogram”中省略了参数名称。我必须对“直方图”功能进行哪些更改?该程序正在读取单词并打印它们的长度(直方图)。
这是 main()
函数:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 99
#define M 99
int main(int argc, char *argv[])
{
int a, j, count = 0, i, b = 0, word = 0, sum = 0;
char x[N][M];
for(i=0; i<N; i++) {
printf("eisagete leksi,\ngrapste ****telos gia exit: ");
scanf("%s", x[i]);
a = strcmp(x[i], "****telos");
if(a==0) break;
count++;
}
for(i=0;i<count;i++)
printf("%d leksi: %s\n",i+1, x[i]);
printf("\n");
for (j=0;j<count;j++){
printf("%d :" , j+1);
for(i=0; i<strlen(x[j]) ;i++){
printf("*");}
printf("\n\n");
}
while ((a=epilogi())!=5){
switch (a){
case 1: metrisileksewn(x);break;
case 2: metrisixaraktirwn(x);break;
case 3: diaflekseis(x);break;
case 4: istograma(x);break;
default:break;
}
}
}
我的功能之一是:
void histogram(char[N][M]){
int i, j, count;
char x[N][M];
for (j=0; j<count; j++){
for(i=0; i<strlen(x[j]); i++)
printf("*");
printf("\n");
}
}
最佳答案
您需要提供参数的名称。
void histogram(char y[N][M])
您可以省略函数原型(prototype)声明中的名称,但不能省略定义。
您还需要考虑是否仍然需要 x
,或者是否应将参数命名为 x
并删除局部变量 - 我认为这可能是您所需要的。但您还必须担心 count
— 它在哪里初始化?它可能应该是函数的额外参数。您应该使用 putchar() 来放置单个字符;当没有包含至少一个转换规范的格式字符串时,最好不要使用 printf()
。
void histogram(char x[][M], int count)
{
for (int j = 0; j < count; j++)
{
int len = strlen(x[j]);
for (int i = 0; i < len; i++)
putchar('*');
putchar('\n');
}
}
关于c - 我的函数中省略了参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23327928/