Simple Challenge C++

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5

71 Replies - 42783 Views - Last Post: 19 July 2013 - 07:04 PM

#31 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Simple Challenge C++

Posted 01 July 2011 - 10:25 AM

Spoiler


If we cannot change any lines, neither can you and 10 is and always will be an even number.

This post has been edited by Karel-Lodewijk: 01 July 2011 - 10:25 AM

Was This Post Helpful? 2
  • +
  • -

#32 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Simple Challenge C++

Posted 01 July 2011 - 10:37 AM

I must admit I laughed when you said 10 will always be an even number. You're absolutely correct but you failed to realized 10 was only used as an example. Do you think your program will work if f was equal to 11? The other examples work no matter what number is sent as the parameter.
Was This Post Helpful? 0
  • +
  • -

#33 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Simple Challenge C++

Posted 01 July 2011 - 10:38 AM

Yes I realized it was just an example :) anyway, a real entry, you can use xor 1 to add 1 to an even number and substract 1 from an odd number:

Spoiler

This post has been edited by Karel-Lodewijk: 01 July 2011 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#34 7HUND3R  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-June 11

Re: Simple Challenge C++

Posted 02 July 2011 - 08:03 AM

The simplest technique is to use a bitwise and operator (&) against 1.
Was This Post Helpful? 0
  • +
  • -

#35 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Simple Challenge C++

Posted 02 July 2011 - 08:05 AM

Yes, we know. The purpose of this thread has since become to find creative solutions.
Was This Post Helpful? 0
  • +
  • -

#36 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: Simple Challenge C++

Posted 29 February 2012 - 03:29 PM

here is my solution:
Spoiler


Now I look forward to reading all of the smart solutions you guys/gals came up with!

This post has been edited by raspinudo: 29 February 2012 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#37 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,535
  • Joined: 09-June 09

Re: Simple Challenge C++

Posted 08 April 2012 - 09:27 PM

Here's a round about way of doing it (literally)
bool isEven(int n) {
	return ((n >> 1) | (n << 31)) < 2147483648;
}


This post has been edited by jjl: 08 April 2012 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#38 vidit.ochani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-April 12

Re: Simple Challenge C++

Posted 27 April 2012 - 05:46 PM

Here's my solution..

Spoiler

Was This Post Helpful? 0
  • +
  • -

#39 dilipkumarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 12

Re: Simple Challenge C++

Posted 06 May 2012 - 12:03 AM

View Postcodeprada, on 01 March 2011 - 04:18 PM, said:

Complete this program in just one line. It tells the user whether a number is even or odd.

Limitations:
  • No use of the Mod(%) operator
  • no use of functions
  • no use of classes
  • no use of / * - +
  • a line is counted by the amount of semicolons used. so your return statement counts as one line
  • you cannot alter any other line in the code


Good luck :bananaman:
#include <iostream>
using namespace std;

bool isEven(int f)
{
    //one line here
}

int main (int argc, char * argv[])
{
    int f = 10;
    cout << f << ( isEven(f)  ?  " is even" : " is odd" ) << endl;
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#40 dilipkumarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 12

Re: Simple Challenge C++

Posted 06 May 2012 - 12:16 AM

Hi every one.. I think I have a Best solution..take a look

SIMPLE... JUST ADD BELOW CODE IN THE isEven()


return !(f%2);


What do you say???

Please post if my solution is wrong ..Ty :)
Was This Post Helpful? 0
  • +
  • -

#41 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 06 May 2012 - 03:28 PM

the challenge disallows the use of '%'
Was This Post Helpful? 0
  • +
  • -

#42 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Simple Challenge C++

Posted 09 May 2012 - 07:39 AM

This is what i can came with:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#43 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,535
  • Joined: 09-June 09

Re: Simple Challenge C++

Posted 10 May 2012 - 06:35 PM

View Postaaa111, on 09 May 2012 - 03:39 PM, said:

This is what i can came with:
Spoiler


Note that the ternary operator is a waste of a conditional check.

since anything anded with 0x01 is either 0x01 or 0x00 (true of false) you can just return the result directly.

This post has been edited by jjl: 10 May 2012 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#44 timkd127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 27-February 10

Re: Simple Challenge C++

Posted 11 May 2012 - 06:53 AM

This was a welcome distraction from studying for my calc 3 final

Spoiler

This post has been edited by jimblumberg: 11 May 2012 - 06:55 AM
Reason for edit:: Fixed tags

Was This Post Helpful? 0
  • +
  • -

#45 alzy101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-May 12

Re: Simple Challenge C++

Posted 22 May 2012 - 12:54 PM

View Postcodeprada, on 01 March 2011 - 11:50 AM, said:

Spoiler


How does this work exactly? (I am pretty novice). I keep trying to break it down but to no avail since I don't even know what the '&' operator does in this expression.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5