Tutorial 48 Problems

0 dan mendez · May 24, 2015
I noticed that in tutorial 48 I was getting the incorrect average when I wrote the code. This happened when I was using typecast to change the integers to floats. When I set "howMany" and "total" to floats in the beginning, I was able to get the correct average. Does anyone know why this is?

Post a Reply

Replies

Oldest  Newest  Rating
0 Laura Lee · May 24, 2015
I have not seen the video but I'll assume what you're asking is why an integer gives an incorrect value?

The answer is simple. Integer as is stated by definition is a whole value. It is a rational of course, over 1.

The same rules apply here.
float f = 3;
int n = 3;

printf("%i\n",n/2);

// 3/2 is 1.5, but n is an integer. Instead, 1 will be printed.

float f = 3;
printf("%f\n",f/2);
//Will print 1.5


When performing averages, you might end up with results that are not whole numbers.
0 Laura Lee · May 24, 2015
I declared f twice by accident. Apologies for that.
0 dan mendez · May 24, 2015
Thank you for the explanation! Makes sense now!
  • 1

C

107,338 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator