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="James Kuyper, post: 4393180"] [i] You can put the increment in the loop, and you did so in the code below. What you shouldn't do is put it in a conditional part of the ternary expression. Your loop's behavior depends only upon the value of 'i'. If, during one pass though the loop, the value of i doesn't change, it will go through the loop again, with the same value of i, and therefore exactly the same behavior - which means it still won't change. Infinite loop. Now, with that in mind, take a look at the code above. If i has a value which is a multiple of 3 or a multiple of 5, the value of i will not be changed. That's why you've got to move the i++ out of the ternary expression, as you did in your code below. No, the first value does not "return false". Your code attempts to read the first 1000 elements of a[], regardless of whether or not it actually has 1000 elements. So what happens is that it tries to read pieces of memory that aren't actually part of a[]. As a result, the behavior of your program is not defined by the C standard. This is bad - very bad. Don't do that. You can never assign an array - what you actually mean, I think, is either "define" or "declare". You can't define an array to have an unknown number of entries. Defining an object requires you to give the compiler all the information it needs to set aside storage space for the object; if you don't tell it how long the array is, the compiler can't do that. When you declared it as int a[], and didn't provide an actual length anywhere else in your program, it implicitly made the length 1. But that's something you should have known; it doesn't count as defining an array of unknown length. What you can do is declare an array to have a number of entries that is not known within your code, but is known SOMEWHERE ELSE. For instance, the simplest change to your code that would allow this is to declare extern int a[]; In some other part of your program, you must provide a definition for a that does have a specific length: int a[1000]; However, using global variables is seldom the best solution. All of your variables are global, and none of them should be. A more appropriate way to do this would be to declare a function, which takes a pointer to the first element of the array, and length: int three_five_sum( int a[], int n ){ // body of function. } That looks like it defines a to be an array, but one of the confusing features of C is that the declaration of an array as a parameter to a function is automatically converted into a declaration of a pointer to the element type of that array. It's exactly the same as if I had written "int *a". Just as a matter of convention, I normally declare parameters which are pointers to the first element of an array using array[], while I write those which are pointers to a single object as *single_object. As far as I can tell, this convention is my own peculiarity, and not in wide use. [i] There are many good uses of the ternary operator; I use it all the time. This isn't one of them. An expression statement containing a ternary expression can usually be re-written using if(). Unless you're using the result of the ternary expression, it's generally better to use the if() version: if( i%3 == 0 || i%5 == 0) sum += a[i]; Of course, the whole point of your exercise was to practice the use of the ternary operator - but you should do that with a problem for which that operator is a reasonable thing to use. Try the following: write a function that takes two pointer arguments, a and b, and a count. For each value of i from 0 to count-1, it adds something to sum. However, if i is a multiple of 3 or 5, it adds a[i] to sum; otherwise, it adds b[i] to sum. You've got a variable named i, which is used only inside the loop, which must be incremented during each step of the loop. When you've got that combination, it's generally better to write it as a for() loop rather than a while loop: for(int i=0; i<1000; i++) If your code has to be compatible with C90, you'll have to break that up into two parts: int i; for(i=0; i<1000; i++)[/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Using ternary and summing array
Top