Author Topic: Coding Help  (Read 4172 times)

0 Members and 2 Guests are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Coding Help
« Reply #15 on: April 03, 2012, 02:00:22 pm »
actually you should use IF/ELSE, because now the code after the break is what people call "unreachable code".
http://en.wikipedia.org/wiki/Unreachable_code
It's not the true definition but you get the point :P

For small script like your this solution works, but for bigger programs this may cause really really bad headaches when debugging. And I know the feel :D

EDIT: xor is right, your requirements are not very clear.
« Last Edit: April 03, 2012, 02:07:39 pm by Kulverstukas »

xor

  • Guest
Re: Coding Help
« Reply #16 on: April 03, 2012, 02:16:21 pm »
It's not unreachable code you numpty. That's the worst definition ever.

This is unreachable code.

Code: [Select]

function doSomething( int x )
{
    if (x == 1)
        return 1;
    else
        return 0;

    return 0; // << unreachable, never ever in a million years will this get executed.
}


His print statement gets executed, which means it's not unreachable code.


This is more like what he should be doing:


Code: [Select]
for (int i=0; i < numberArray.length; i++) // Loop through all of the array elements
{
    if ( (numberArray[i] % 2) == 0) // if the current element is divisible by two and has no remainder (an even number)
    {
        if (numberArray[i] < 412) // if the number is less than 412
        {
            print numberArray[i]; // print it out
        }
    }
}


This can also be optimized further by performing the modulo check at the same time as the number size by using an AND operator


Code: [Select]

for (int i=0; i < numberArray.length; i++) // Loop through all of the array elements
{
    if ( ((numberArray[i] % 2) == 0) && numberArray[i] < 412 ) // if the current element is divisible by two and has no remainder
    {                                                          // (an even number)  AND (&&) is less than 412
        print numberArray[i]; // print it out
    }
}
« Last Edit: April 03, 2012, 02:22:26 pm by xor »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Coding Help
« Reply #17 on: April 03, 2012, 03:32:54 pm »
I don't see any ambiguity in the task.

Quote
as soon as your program hits 412 it will stop

That is exactly what it shall do. It says:

Quote
but only up to the number "412" (not including it) Stop there and ignore all numbers past 412 in the sequence

xor

  • Guest
Re: Coding Help
« Reply #18 on: April 03, 2012, 06:02:15 pm »
I'm basing it on the assumption that his native language isn't English and the fact that he also said:  (Note there will be numbers over 412.)