3

Every time I execute my program which finds the average in a ten element array. I get slightly different results. Any idea why?

Here is my code:

#include "stdio.h"

int main()
{
float array[10];

for (int n=0; n<10;n++)
{
    array[n] = n * 4.76;
    printf("array[%i] = %.4f\n",n,array[n] );
}

float total;
for (int n=0; n<10; n++)
{
    total = total + array[n];
}

printf("Average: %.4f\n", total/10 );
return 0;
}

and some sample results are:

array[0] = 0.0000
array[1] = 4.7600
array[2] = 9.5200
array[3] = 14.2800
array[4] = 19.0400
array[5] = 23.8000
array[6] = 28.5600
array[7] = 33.3200
array[8] = 38.0800
array[9] = 42.8400
Average: 21.2598

array[0] = 0.0000
array[1] = 4.7600
array[2] = 9.5200
array[3] = 14.2800
array[4] = 19.0400
array[5] = 23.8000
array[6] = 28.5600
array[7] = 33.3200
array[8] = 38.0800
array[9] = 42.8400
Average: 21.2826
5
  • 7
    Hint: What value does total contain just before the second for loop? Commented Jul 3, 2016 at 4:11
  • int total=0; You sir are a genius. Thank you. Was it generating a random number since it did not have an initial value? Commented Jul 3, 2016 at 4:13
  • 1
    If you make it an int then it can only hold whole numbers. Commented Jul 3, 2016 at 4:15
  • got too excited meant to write float lol Commented Jul 3, 2016 at 4:23
  • @BackSlash Uninitialized variables do not generate random values. Commented Jul 3, 2016 at 5:55

1 Answer 1

6

When declaring variables in C, make sure they're initialised to a default value. Variables allocated on the stack are usually not initialised to their default values, rather, they are initialised with junk.

So, before starting summation, initialise your variable as

float total = 0.0f;

and you should get the same answer every time.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you know why this is the case ? where do these random numbers come from ?
Before your program was using the stack, there's a large possibility some other program was. And that program may have used THAT memory location in particular, and once it was done, didn't clean up after itself (it doesn't need to). And now when your program took that memory location for its own purposes, C didn't bother to set the value to 0 by default (it's an unnecessary overhead).
@BackSlash It's just whatever was left in the memory used for the variable... could be anything, depending on what it was used for last (possibly not even by your program, but sometimes by another part of your program that isn't using it anymore such as a function call that has returned already).
I see, nice to know how things are working under the hood. Where can I learn more ? have you read or watched anything lately that that might help me understand how memory allocation is used in C?
@Shiva: if the stack contains information from another program, the system has a serious security issue. It may, however, have material left over from the start up code executed before main() is called (the dynamic loader, for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.