Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Using ternary and summing array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="flebber, post: 4393167"] 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] :i++ ; never actually finished, I think I created an infinite loop. PS I was following the example from here [URL]http://www.tutorialspoint.com/ansi_c/ternary_operator_examples.htm[/URL] 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[i] :0 ; i++; } printf("The sum is %d\n",sum); return 0; } Sayth[/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Using ternary and summing array
Top