F
flebber
Hi
I am learning C. I am trying to sum an array with the values returned
from a loop. Can I use the "?" operator as part of a loop; by
experimentation I know I can't put the increment in the loop.
doing ((i % 3 == 0)||(i % 5 == 0)) ? sum+=a :i++ ; never actually
finished, I think I created an infinite loop.
PS I was following the example from here
http://www.tutorialspoint.com/ansi_c/ternary_operator_examples.htm
So I have come to this this solution, I think the problem is that my
array a[] is assigned as a one element array and since the first value
returns false it gets 0 and exits. Is this right? If it is how do i
assign an array to have an unknown number of entries?
A kick in the right direction appreciated
#include <stdio.h>
int a[],i,sum=0;
int main()
{
while(i < 1000)
{
((i % 3 == 0)||(i % 5 == 0)) ? sum+=a :0 ;
i++;
}
printf("The sum is %d\n",sum);
return 0;
}
Sayth
I am learning C. I am trying to sum an array with the values returned
from a loop. Can I use the "?" operator as part of a loop; by
experimentation I know I can't put the increment in the loop.
doing ((i % 3 == 0)||(i % 5 == 0)) ? sum+=a :i++ ; never actually
finished, I think I created an infinite loop.
PS I was following the example from here
http://www.tutorialspoint.com/ansi_c/ternary_operator_examples.htm
So I have come to this this solution, I think the problem is that my
array a[] is assigned as a one element array and since the first value
returns false it gets 0 and exits. Is this right? If it is how do i
assign an array to have an unknown number of entries?
A kick in the right direction appreciated
#include <stdio.h>
int a[],i,sum=0;
int main()
{
while(i < 1000)
{
((i % 3 == 0)||(i % 5 == 0)) ? sum+=a :0 ;
i++;
}
printf("The sum is %d\n",sum);
return 0;
}
Sayth