• Agent641@lemmy.world
    link
    fedilink
    arrow-up
    80
    ·
    2 years ago

    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.

    • elauso@feddit.de
      link
      fedilink
      arrow-up
      24
      ·
      2 years ago

      Yeah, “just use modulo” - no shit, you must be some kind of master programmer

      • Karyoplasma@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        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.

  • GoosLife@lemmy.world
    link
    fedilink
    arrow-up
    23
    ·
    edit-2
    2 years ago

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

    #include 
    #include 
    
    
    int main()
    {
    	int number = 0;
    	int numberToAdd = 1;
    	int modifier = 1;
    
    	std::cout << "Is your number [p]ositive or [n]egative? (Default: positive)\n";
    	if (std::cin.get() == 'n') {
    		modifier *= -1;
    	}
    
    	std::cin.ignore(std::numeric_limits::max(), '\n'); // Clear the input buffer
    
    	bool isEven = true;
    	bool running = true;
    
    	while (running) {
    		std::cout << number << " is " << (isEven ? "even" : "odd") << ".\n";
    		std::cout << "Continue? [y/n] (Default: yes)\n";
    
    		if (std::cin.peek() == 'n') {
    			running = false;
    		}
    
    		number += numberToAdd * modifier;
    		isEven = !isEven;
    
    		std::cin.ignore(std::numeric_limits::max(), '\n');
    	}
    
    	std::cout << "Your number, " << number << " was " << (isEven ? "even" : "odd") << ".\n";
    }```
      • GoosLife@lemmy.world
        link
        fedilink
        arrow-up
        7
        ·
        2 years ago

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

        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        
        template 
        bool isEven(const T& number) {
            std::vector digits;
            std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
            std::uniform_int_distribution distribution(1, 9);
        
            std::string numberStr = std::to_string(number);
        
            for (char digit : numberStr) {
                digits.push_back(distribution(generator));
            }
        
            int sum = std::accumulate(digits.begin(), digits.end(), 0);
        
            return sum % 2 == 0;
        }
        
  • Rentlar@lemmy.ca
    link
    fedilink
    arrow-up
    19
    ·
    2 years ago

    They call me a StackOverflow expert:

    private bool isEven(int num) {
    if (num == 0) return true;
    if (num == 1) return false;
    if (num < 0) return isEven(-1 * num);
    return isEven(num - 2);
    }
    
  • jsdz@lemmy.ml
    link
    fedilink
    Interlingua
    arrow-up
    13
    ·
    edit-2
    2 years ago
    int is_even(int n)
    {
        int result = -1;
        char number[8]; //should be enough
        sprintf(number, "%d", n);
    
        // check the number
        // TODO: handle negative numbers
        for (char *p=number; *p; p++)
        {
            if (*p=='0' || *p=='2' || *p=='4' || *p=='6' || *p=='8')
                result = 1;
            else if (*p=='1' || *p=='3' || *p=='5' || *p=='7' || *p=='9')
                result = 0;
            else {
               fprintf(stderr, "Your number is wrong!\n");
               exit(1); 
            }
        }
        return result;
    }
    
    • Whimsical@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      Got it boss

      (Quietly implements a modulo check but only for a range between the current endpoint of the if branches and the highest value I expect the product to ever encounter)

  • wtry@lemm.ee
    link
    fedilink
    arrow-up
    10
    arrow-down
    3
    ·
    2 years ago

    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@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      18
      ·
      2 years ago

      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?

      • Clearwatermo@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        2 years ago

        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@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          1
          ·
          2 years ago

          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.

        • rustbuckett@lemmings.world
          link
          fedilink
          arrow-up
          4
          arrow-down
          1
          ·
          2 years ago

          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@lemmy.world
            link
            fedilink
            arrow-up
            8
            ·
            2 years ago

            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.

      • EvokerKing@lemmy.world
        link
        fedilink
        arrow-up
        7
        arrow-down
        3
        ·
        2 years ago

        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@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          4
          ·
          edit-2
          2 years ago

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

  • Skyline969@lemmy.ca
    link
    fedilink
    English
    arrow-up
    6
    ·
    2 years ago

    Wow. Amateur hour over here. There’s a much easier way to write this.

    A case select:

    select(number){
        case 1:
            return false;
        case 2:
            return true;
    }
    

    And so on.