Flax_vert,

Rip switch statements

pseudo,
@pseudo@jlai.lu avatar

Gott ist gnädig.

wtry,

I can’t believe he needs that much code for this: bool iseven(int number){ if (number % 2 == 0){ return true; } else { return false; } }

TopRamenBinLaden,

I like the example in the post better. It is more clear as to what is going on to an experienced dev like me. What’s this 2 percent nonsense?

filcuk,

Readability over obscure hacks

rustbuckett,

It’s not obscure. This is the example, with syntactic differences, for this problem in almost every programming book I’ve read. He just didn’t include newlines.

mob,

At this point, I really can’t tell who’s joking around or who’s being serious in this thread.

Shits cracking me up though reading this all as serious discussion.

TopRamenBinLaden,

Hey now, this is a serious academic conversation about the 2 percent operator.

Clearwatermo,

I like the example in the comment better. It is more confusing as to what is going on to an experienced dev like me. iSeven is always odd tho right?

TopRamenBinLaden,

I think you are on to something there. Personally, I just don’t see the advantage of using iSeven over iSix, though. I might start using iEight whenever they finally iron the kinks out of that one.

EvokerKing,

Explanation: the percent is modulus. Basically it’s just divide the first number by the second and return the remainder. If you do number % 2, it will return 1 if it is odd and 0 if it is even. For example 4/2 has a remainder of 0 and therefore is even. 3/2 has a remainder of 1, and therefore is odd.

TopRamenBinLaden, (edited )

Sorry I should’ve put the /s. I was just playing. But thank you for the helpful explanation, nonetheless. You are a nice person.

infreq,

Whooosh

Kingofthevoid,

Whoosh

Clbull,

I, EvaX, humbly submit a toast to Nicholas Alexander for successfully managing to pirate WarCraft III so that he may play Defense of the Ancients.

Congratulations Nick. Enjoy your DotA!

(sips from milk goblet)

wtry,

*cum chalice

Clbull,
DeathsEmbrace,

You can just divide the number by 2 and if you get a whole number it’s true and if it’s a decimal it’s false…

veni_vedi_veni,

C’mon now, we don’t all have quantum computers to do division

STUPIDVIPGUY,

huh? that stupid

jsdz, (edited )

<span style="color:#323232;">int is_even(int n)
</span><span style="color:#323232;">{
</span><span style="color:#323232;">    int result = -1;
</span><span style="color:#323232;">    char number[8]; //should be enough
</span><span style="color:#323232;">    sprintf(number, "%d", n);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    // check the number
</span><span style="color:#323232;">    // TODO: handle negative numbers
</span><span style="color:#323232;">    for (char *p=number; *p; p++)
</span><span style="color:#323232;">    {
</span><span style="color:#323232;">        if (*p=='0' || *p=='2' || *p=='4' || *p=='6' || *p=='8')
</span><span style="color:#323232;">            result = 1;
</span><span style="color:#323232;">        else if (*p=='1' || *p=='3' || *p=='5' || *p=='7' || *p=='9')
</span><span style="color:#323232;">            result = 0;
</span><span style="color:#323232;">        else {
</span><span style="color:#323232;">           fprintf(stderr, "Your number is wrong!n");
</span><span style="color:#323232;">           exit(1); 
</span><span style="color:#323232;">        }
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">    return result;
</span><span style="color:#323232;">}
</span>
AnxiousDuck,

This should be the accepted answer

EvokerKing,

Why not one with modulus?

MolochAlter,

This one is more readable.

GoosLife, (edited )

There is absolutely no need to add a check for each individual number, just do this:


<span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">()
</span><span style="color:#323232;">{
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> number </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">;
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> numberToAdd </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">1</span><span style="color:#323232;">;
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> modifier </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">1</span><span style="color:#323232;">;
</span><span style="color:#323232;">
</span><span style="color:#323232;">	std::cout </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">"Is your number [p]ositive or [n]egative? (Default: positive)</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">;
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(std::cin.get() </span><span style="font-weight:bold;color:#a71d5d;">== </span><span style="color:#183691;">'n'</span><span style="color:#323232;">) {
</span><span style="color:#323232;">		modifier </span><span style="font-weight:bold;color:#a71d5d;">*= -</span><span style="color:#0086b3;">1</span><span style="color:#323232;">;
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">
</span><span style="color:#323232;">	std::cin.ignore(std::numeric_limits::max(), </span><span style="color:#183691;">'</span><span style="color:#0086b3;">n</span><span style="color:#183691;">'</span><span style="color:#323232;">); </span><span style="font-style:italic;color:#969896;">// Clear the input buffer
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">bool</span><span style="color:#323232;"> isEven </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">true</span><span style="color:#323232;">;
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">bool</span><span style="color:#323232;"> running </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">true</span><span style="color:#323232;">;
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">while </span><span style="color:#323232;">(running) {
</span><span style="color:#323232;">		std::cout </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; number </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">" is " </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; (isEven </span><span style="font-weight:bold;color:#a71d5d;">? </span><span style="color:#183691;">"even" </span><span style="font-weight:bold;color:#a71d5d;">: </span><span style="color:#183691;">"odd"</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">".</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">;
</span><span style="color:#323232;">		std::cout </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">"Continue? [y/n] (Default: yes)</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">;
</span><span style="color:#323232;">
</span><span style="color:#323232;">		</span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(std::cin.peek() </span><span style="font-weight:bold;color:#a71d5d;">== </span><span style="color:#183691;">'n'</span><span style="color:#323232;">) {
</span><span style="color:#323232;">			running </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">false</span><span style="color:#323232;">;
</span><span style="color:#323232;">		}
</span><span style="color:#323232;">
</span><span style="color:#323232;">		number </span><span style="font-weight:bold;color:#a71d5d;">+=</span><span style="color:#323232;"> numberToAdd </span><span style="font-weight:bold;color:#a71d5d;">*</span><span style="color:#323232;"> modifier;
</span><span style="color:#323232;">		isEven </span><span style="font-weight:bold;color:#a71d5d;">= !</span><span style="color:#323232;">isEven;
</span><span style="color:#323232;">
</span><span style="color:#323232;">		std::cin.ignore(std::numeric_limits::max(), </span><span style="color:#183691;">'</span><span style="color:#0086b3;">n</span><span style="color:#183691;">'</span><span style="color:#323232;">);
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">
</span><span style="color:#323232;">	std::cout </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">"Your number, " </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; number </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">" was " </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; (isEven </span><span style="font-weight:bold;color:#a71d5d;">? </span><span style="color:#183691;">"even" </span><span style="font-weight:bold;color:#a71d5d;">: </span><span style="color:#183691;">"odd"</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt;</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">lt; </span><span style="color:#183691;">".</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">;
</span><span style="color:#323232;">}```
</span>
trashgirlfriend,

I hate this

GoosLife,

Sorry, let me try again. Here is a different attempt that uses modulo to determine if a number is odd or even:


<span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="font-weight:bold;color:#a71d5d;">#include 
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">template 
</span><span style="font-weight:bold;color:#a71d5d;">bool </span><span style="color:#323232;">isEven(</span><span style="font-weight:bold;color:#a71d5d;">const</span><span style="color:#323232;"> T</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">amp; number) {
</span><span style="color:#323232;">    std::vector digits;
</span><span style="color:#323232;">    std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
</span><span style="color:#323232;">    std::uniform_int_distribution distribution(</span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="color:#0086b3;">9</span><span style="color:#323232;">);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    std::string numberStr </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">std::to_string(number);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">char</span><span style="color:#323232;"> digit </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;"> numberStr) {
</span><span style="color:#323232;">        digits.push_back(distribution(generator));
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> sum </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">std::accumulate(digits.begin(), digits.end(), </span><span style="color:#0086b3;">0</span><span style="color:#323232;">);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">return</span><span style="color:#323232;"> sum </span><span style="font-weight:bold;color:#a71d5d;">% </span><span style="color:#0086b3;">2 </span><span style="font-weight:bold;color:#a71d5d;">== </span><span style="color:#0086b3;">0</span><span style="color:#323232;">;
</span><span style="color:#323232;">}
</span>
nogrub,

i think you forgot an include there also a goto statement would work better

superpants,

I did something like this in high school

CancerMancer,

Because YandereDev is a legendary moron I can’t even tell if this is a joke or not.

mob,

How do you think even/odd detectors work? A team of coders has been working on this else if for years…

If you want to help

github.com/samuelmarina/is-even

mbp,
@mbp@lemmy.sdf.org avatar

I haven’t ever seen a GitHub file that big before in my life

Flax_vert,

Would be easier to make a script to write that script honestly

CancerMancer,

imagine generating this at runtime

MolochAlter,

It’s a re-attribution of a joke tweet made by someone else.

dylanTheDeveloper,
@dylanTheDeveloper@lemmy.world avatar

Now make it a switch case

pinkdrunkenelephants,

Modulus equals zero anyone?

ramenshaman,

That’s the joke

nogrub,

but that operation is expensive

pinkdrunkenelephants,

So what? It works and it’s better than precompiling a list of all known even and odd numbers, and expecting a computer to iterate through a whole list every time it wants to check the value of something.

The stupid trig tables are just as problematic and it’s why graphics chips use other geometric functions instead. It’s just better to use a modulus.

pixeltree,

No I’m pretty sure an if else ladder is more better

Rubeer,

Nah, it compiles down to an AND because of the constant 2

Smacks,
@Smacks@lemmy.world avatar

Still some of YandereDev’s best code

Karyoplasma,

<span style="color:#323232;">while (true){
</span><span style="color:#323232;">    if (number == 0) return true;
</span><span style="color:#323232;">    if (number == 1) return false;
</span><span style="color:#323232;">    number -= 2
</span><span style="color:#323232;">}
</span>
SamBBMe,

return !(number % 2)

stevep,

Setting number to -1 might cause you to wait a while.

Karyoplasma,

You know, shortly after posting this I did think about whether it’s still working if I just pass the underflow that will happen at some point or if I have to fix something in that case (like subtract 1 after the underflow). I deemed it “too complicated” and would just issue a warning that my code is only tested on positive numbers, but I think it will still work.

Agent641,

Just print True all the time. Half the time it will be correct and the client will be happy, and the other half the time, they will open a ticket that will be marked as duplicate and closed.

Rouxibeau,

Reminds me of the fake thermometers being sold during the peak of COVID that weren’t actually thermometers but just displayed numbers to make people think they were.

Not_Alec_Baldwin,

I definitely have one of these.

callyral,
@callyral@pawb.social avatar

number == 0 is not handled

trafficnab,

You can’t see it but there’s an “else return true;” at the bottom

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines