程序不返回浮点并根据使用的编译器给出不同的结果? #包括 #包括 #包括 无效平均猜测(int); int main() { int i,userInput,compGuess,totalGuess,loopGuess=0; srand(时间(空)); printf(“请输入一个介于0和99之间的数字:\n”); scanf(“%d”、&userInput); 对于(i=0;i

程序不返回浮点并根据使用的编译器给出不同的结果? #包括 #包括 #包括 无效平均猜测(int); int main() { int i,userInput,compGuess,totalGuess,loopGuess=0; srand(时间(空)); printf(“请输入一个介于0和99之间的数字:\n”); scanf(“%d”、&userInput); 对于(i=0;i,c,loops,codeblocks,C,Loops,Codeblocks,程序的目标是打印出一个浮点,但我得到的是整数。我已经用代码块和在线C编译器编译了它,但后者给了我负数,而代码块不返回浮点值 无法判断这是否是我的代码或编译器的问题。这里的代码有问题: #include <stdio.h> #include <stdlib.h> #include<time.h> void averageGuess(int); int main() { int i, userInput, compGuess, totalGuess, l

程序的目标是打印出一个浮点,但我得到的是整数。我已经用代码块和在线C编译器编译了它,但后者给了我负数,而代码块不返回浮点值


无法判断这是否是我的代码或编译器的问题。

这里的代码有问题:

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

void averageGuess(int);
int main()
{
    int i, userInput, compGuess, totalGuess, loopGuess = 0;
    srand(time(NULL));

    printf("Please enter a number between 0 and 99: \n");
    scanf("%d", &userInput);

    for(i = 0; i < 50; i++)
    {
        loopGuess = 0;
        do
        {
            compGuess = (rand() % 100);
            loopGuess++;
        } while(compGuess != userInput);

        totalGuess += loopGuess;
    }
    averageGuess(totalGuess);
    return 0;
}//end main

void averageGuess(int totalGuess)
{
    float average;
    average = totalGuess / 50;
    printf("The program took an average of %lf random number generations to match the target number over the 50 experiments.", average);
}//end function
“/”运算符看到它必须除以的两个整数,因此返回一个整数。如果需要将结果设置为浮点,请使用50.0而不是50

我用代码块和在线C编译器编译了它,但后者给了我负数

这一行:

    average = totalGuess / 50;
不是将所有变量都设置为
0
,只是
loopGuess
。因此,
totalGuess
没有初始化,这就解释了 不同编译器之间的行为差异(以及错误结果,
totalGuess
的初始值是随机的,可能是负数)。修正:

int i, userInput, compGuess, totalGuess, loopGuess = 0;
程序的目标是打印出一个浮点,但我得到的只是整数

然后使用浮点除法,而不是整数除法

gcc没有警告未初始化的变量(糟糕!),但是clang警告了(甚至建议了正确的修复,哇!)


该代码有两个问题:

a)
totalGuess
除以整数,因此结果四舍五入为
int

average=totalGuess/50

b) 负数来自以下事实:
totalGuess
变量未初始化,因此它可以是任何数字(如开头的大负数)

修正程序:

S:\>gcc -Wall -Wextra test.c
(no warnings!!)
S:\>clang -Wall -Wextra test.c

test.c:23:9: warning: variable 'totalGuess' is uninitialized when used here
      [-Wuninitialized]
        totalGuess += loopGuess;
        ^~~~~~~~~~
test.c:8:44: note: initialize the variable 'totalGuess' to silence this warning
    int i, userInput, compGuess, totalGuess, loopGuess = 0;
                                           ^
                                            = 0
#包括
#包括
#包括
void averageGuess(int totalGuess)
{
浮动平均;
平均值=总猜测值/50.0;
printf(“该程序平均花费%.2f个随机数生成来匹配50次实验中的目标数。”,平均值);
}
int main()
{
inti,用户输入,compGuess;
int-totalGuess=0;
双环猜测=0;
srand(时间(空));
printf(“请输入一个介于0和99之间的数字:\n”);
scanf(“%d”、&userInput);
对于(i=0;i<50;i++)
{
loopGuess=0;
做
{
compGuess=(rand()%100);
loopGuess++;
}while(compGuess!=用户输入);
totalGuess+=循环猜测;
}
平均猜测(总猜测);
返回0;
}//端干管

好吧,你所有的变量都是int类型的
int
;那么,为什么您希望程序打印一个
float
?totalGuess是一个整数,50是一个整数,因此除法是作为一个整数进行的。请尝试改用50.0,或者将totalGuess强制转换为float。@此表达式totalGuess/50的计算结果是一个整数表达式。改为写totalGuess/50.0f@同样,封闭的循环也没有意义。@Vladfrommosco这是作业说明,但谢谢!这个操作如何按照OP的要求给出负数?因为totalGuess没有初始化为0,并且在开始时可以包含负数。见Jean-François Fabre的评论。
float average;
average = totalGuess / 50.0;
S:\>gcc -Wall -Wextra test.c
(no warnings!!)
S:\>clang -Wall -Wextra test.c

test.c:23:9: warning: variable 'totalGuess' is uninitialized when used here
      [-Wuninitialized]
        totalGuess += loopGuess;
        ^~~~~~~~~~
test.c:8:44: note: initialize the variable 'totalGuess' to silence this warning
    int i, userInput, compGuess, totalGuess, loopGuess = 0;
                                           ^
                                            = 0
#include <stdio.h>
#include <stdlib.h>
#include<time.h>

void averageGuess(int totalGuess)
{
    float average;
    average =  totalGuess / 50.0;
    printf("The program took an average of %.2f random number generations to match the target number over the 50 experiments.", average);
}

int main()
{
    int i, userInput, compGuess;
    int totalGuess = 0;
    double loopGuess = 0;

    srand(time(NULL));

    printf("Please enter a number between 0 and 99: \n");
    scanf("%d", &userInput);

    for(i = 0; i < 50; i++)
    {
        loopGuess = 0;
        do
        {
            compGuess = (rand() % 100);
            loopGuess++;

        } while(compGuess != userInput);

        totalGuess += loopGuess;
    }

    averageGuess(totalGuess);
    return 0;
}//end main