I want to initialise an array called 'speed' which has values [0 0.5 1 1.5 ... up to 50]. I'm testing it for 5 values in the array. I know I could do it manually but I would prefer to be able to change it easily later.
My current code is giving me no error messages when I compile it but I'm getting a segmentation fault when running it and I'm not sure why. I know segmentation fault is due to memory problems but am unsure where I'm going wrong - I think it's probably with the 'i+1' section. The lack of displaying 'i' is a bit odd..
Code:
/* Relevant section */
#include <stdio.h>
#include <stdlib.h>
float speed[5]={'\0'};
int i=0;
for (i=0; i<5;)
{
speed[0]=0;
speed[i+1]=speed[i]+0.5;
printf("Speed array: %f\n",speed[i]);
printf("i=\n",i);
i++;
}
What it prints:
Code:
Speed array: 0.000000
i=
Speed array: 0.500000
i=
Speed array: 1.000000
i=
Speed array: 1.500000
i=
Speed array: 2.000000
i=
Segmentation fault
Any advice would be gratefully received!