Computer Science Canada Need help simplifying code, for loops, ifs and arrays |
Author: | darkn00b [ Wed Oct 21, 2009 3:57 pm ] | ||
Post subject: | Need help simplifying code, for loops, ifs and arrays | ||
I want to simplify the 5th last line. There are a lot of ands in there, but only one number changes between each of them. I'm pretty sure this can be simplified, but I don't know how. I'm using the latest version of Turing, any other suggestions are welcome too. Thank you
|
Author: | OneOffDriveByPoster [ Wed Oct 21, 2009 7:07 pm ] |
Post subject: | Re: Need help simplifying code, for loops, ifs and arrays |
Hint: you can use a "else" and "for" and then "if" where you have the else-if. You can also use a function. |
Author: | darkn00b [ Wed Oct 21, 2009 7:48 pm ] |
Post subject: | RE:Need help simplifying code, for loops, ifs and arrays |
I tried splitting the elsif into an else, then a for loop (i: 1..7) and then the if, replacing the changing number with i, but that doesn't work because it has to be greater than or equal to all the values, not one at a time. I don't know how I could use a function in there either... |
Author: | TheGuardian001 [ Wed Oct 21, 2009 10:52 pm ] |
Post subject: | Re: Need help simplifying code, for loops, ifs and arrays |
Well, if you need them all to be true and don't want to check them all at once all you need to do is keep track of whether or not they're all right. You could do that with a counter or a boolean, then just check the final value of said counter/boolean. |
Author: | darkn00b [ Thu Oct 22, 2009 3:21 pm ] |
Post subject: | RE:Need help simplifying code, for loops, ifs and arrays |
Thank you the guardian! I did the same thing that I said in my last post, except I added a counter and checked if the counter = 7. Thanks for the great help guys! |