程序不返回 float 并根据使用的编译器给出不同的结果?

Program not returning float and giving different results depending on compiler used?

#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

程序的目标是打印出一个浮点数,但我得到的只是整数。我已经在 Codeblocks 和在线 C 编译器中编译了它,但后者给了我 negative numbers 而 Codeblocks doesn't return a float.

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

这里的代码有问题:

    average = totalGuess / 50;

'/' 运算符看到它必须除以两个整数,因此 returns 一个整数。如果您需要结果为浮点数,请使用 50.0 而不是 50

I've compiled it in Codeblocks and an Online C compiler but the latter is giving me negative numbers

这一行:

int i, userInput, compGuess, totalGuess, loopGuess = 0;

而不是将所有变量设置为0,只是loopGuess。所以 totalGuess 没有初始化,这解释了 不同编译器之间的行为差​​异(和错误结果,totalGuess 的初始值是随机的,可以是负数)。修复:

int i, userInput, compGuess, totalGuess = 0, loopGuess = 0;

The goal is for the program to print out a float but all I get is integers

那就用浮点数除法,而不是整数除法

float average;
average = totalGuess / 50.0;

gcc 不会警告未初始化的变量(不好!)但 clang 会警告(甚至建议正确的修复,哇!):

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

代码有两个问题:

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

average = totalGuess / 50;

b) 负数是因为 totalGuess 变量未初始化,因此它可以是任何数字(例如开头的大负数)。

更正的程序:

#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