-1
#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

The goal is for the program to print out a float but all I get is integers. I've compiled it in Codeblocks and an Online C compiler but the latter is giving me negative numbers while Codeblocks doesn't return a float.

Can't tell if it is an issue with my code or compiler.

sg7
  • 6,108
  • 2
  • 32
  • 40

3 Answers3

0

It is an issue with the code here:

    average = totalGuess / 50;

The '/' operator sees two integers it has to divide and hence returns an integer. If you need the result to be a float use 50.0 instead of 50

Jesferman
  • 1,049
  • 7
  • 12
  • And how this operation can give the negative number as it was asked by OP? – sg7 Apr 01 '18 at 17:56
  • Because totalGuess is not initializated to 0 and can contain a negative number at beginning. See Jean-François Fabre comment. – Jesferman Apr 01 '18 at 18:02
0

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

this line:

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

is not setting all variables to 0, just loopGuess. So totalGuess isn't initialized, which explains the difference in behaviour (and wrong result, initial value of totalGuess is random, can be negative) between different compilers. Fix:

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

Then use floating point division, not integer division

float average;
average = totalGuess / 50.0;

gcc doesn't warn about the uninitialized variable (bad!) but clang does (and even suggests the right fix, wow!):

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
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

There are two issues with the code:

a) totalGuess is divided by integer so the result is rounded to int value

average = totalGuess / 50;

b) negative numbers comes from the fact that totalGuess variable is not initialized so it can be any number (like big negative to start with).

Corrected program:

#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
sg7
  • 6,108
  • 2
  • 32
  • 40