O
OJ
Hi,
as a newbie, could someone explain the "? :" operator to me...
I wrote this as part of a small routine to fill an array with the first
20 items of the Fibonacci sequence, but it produces strange results...
int arrFib[20];
int i,k;
for(i=0;i<20;i++);
{(i<2 ? arrFib = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}
If I write it like this it is correct...
int i;
int fib[20];
fib[0] = 1;
fib[1] = 1;
for (i = 2; i < 20; i++)
{
fib = fib[i-1] + fib[i-2];}
I don't see the difference...
Thanks,
OJ
as a newbie, could someone explain the "? :" operator to me...
I wrote this as part of a small routine to fill an array with the first
20 items of the Fibonacci sequence, but it produces strange results...
int arrFib[20];
int i,k;
for(i=0;i<20;i++);
{(i<2 ? arrFib = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}
If I write it like this it is correct...
int i;
int fib[20];
fib[0] = 1;
fib[1] = 1;
for (i = 2; i < 20; i++)
{
fib = fib[i-1] + fib[i-2];}
I don't see the difference...
Thanks,
OJ