Simple Challenge C++

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

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

#16 milawynsrealm  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 23
  • Joined: 12-May 11

Re: Simple Challenge C++

Posted 13 May 2011 - 10:06 AM

View Postmilawynsrealm, on 12 May 2011 - 06:53 PM, said:

Here's mine:

bool isEven(int f)
{
	return ((f & 1) ? true : false);
}



Sorry, didn't realize that Mod and & were the samething. I figured it was some sort of function. If I post another, it will be the correct version.
Was This Post Helpful? 0
  • +
  • -

#17 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Simple Challenge C++

Posted 14 May 2011 - 01:02 AM

View Postmilawynsrealm, on 13 May 2011 - 06:06 PM, said:

View Postmilawynsrealm, on 12 May 2011 - 06:53 PM, said:

Here's mine:

bool isEven(int f)
{
	return ((f & 1) ? true : false);
}



Sorry, didn't realize that Mod and & were the samething. I figured it was some sort of function. If I post another, it will be the correct version.
Mod is not the same thing as &. In the context which you're using it, the & symbol represents the bitwise-AND operator.
Was This Post Helpful? 0
  • +
  • -

#18 quantizer  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 12
  • Joined: 16-May 11

Re: Simple Challenge C++

Posted 16 May 2011 - 08:46 AM

Spoiler

Was This Post Helpful? 0
  • +
  • -

#19 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Simple Challenge C++

Posted 19 May 2011 - 05:55 AM

I hope I'm not too late to the game guys, this is the simplest method I could think of:

Spoiler


Actually scrap that this also works, but with less characters of code! (I'm sure the other people in this thread have come up with much more elegant solutions.) :)

Spoiler

This post has been edited by RetardedGenius: 19 May 2011 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#20 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 19 May 2011 - 01:29 PM

nope, two others(including me) have the same solution as your second one. even more if you count variations.

it's pretty well as elegant as it gets.

another challenge with well known solution, find 1 expression that checks weather an integer is a power of 2.

int x = ?;
if(<expression>) {
   //'x' is a power of 2;
}



find "<expression>"

This post has been edited by ishkabible: 19 May 2011 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#21 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: Simple Challenge C++

Posted 19 May 2011 - 01:42 PM

Spoiler


Is that it?
Was This Post Helpful? 0
  • +
  • -

#22 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Simple Challenge C++

Posted 19 May 2011 - 02:30 PM

I found that slightly harder, anyway here's my solution:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#23 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 20 May 2011 - 08:25 AM

yes both are correct, the solution i was referring to was RevTorA though, they're both basically the same however. i prefer using the '!' operator instead of '== 0', seems cleaner to me.

Spoiler

This post has been edited by ishkabible: 20 May 2011 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#24 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Simple Challenge C++

Posted 20 May 2011 - 04:39 PM

View Postishkabible, on 20 May 2011 - 04:25 PM, said:

yes both are correct, the solution i was referring to was RevTorA though, they're both basically the same however. i prefer using the '!' operator instead of '== 0', seems cleaner to me.

Spoiler

That'd definitely win you a round of Code golf, although you may want to add an extra right-parenthesis! ;)
Was This Post Helpful? 0
  • +
  • -

#25 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 20 May 2011 - 08:38 PM

o yea... oops :blush:
Was This Post Helpful? 0
  • +
  • -

#26 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Simple Challenge C++

Posted 29 May 2011 - 03:00 PM

View Postquantizer, on 16 May 2011 - 09:46 AM, said:

Spoiler


:no:
Was This Post Helpful? 0
  • +
  • -

#27 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 29 May 2011 - 05:07 PM

lol, we will say it works for now :)

edit:
dose lisp count?
(defun(x)(evenp x))

This post has been edited by ishkabible: 29 May 2011 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#28 lakhbir.lk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-June 11

Re: Simple Challenge C++

Posted 04 June 2011 - 01:32 PM

View Postcodeprada, on 01 March 2011 - 09:18 AM, 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)
{
    return(!(f&1));
}

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


its simple you have to look for last bit i.e if last bit of the number is 1 then it is odd number otherwise it is an even.

This post has been edited by lakhbir.lk: 04 June 2011 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#29 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Simple Challenge C++

Posted 06 June 2011 - 11:51 AM

Yes it is simple, note the title, but you must do it in one line like the others did.
Was This Post Helpful? 0
  • +
  • -

#30 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Simple Challenge C++

Posted 07 June 2011 - 07:35 AM

I tried to think outside the box on this one but when I read back over the rules my solution didn't really fit the requirements:
Spoiler


SO... I worked on a second one:
Spoiler

This post has been edited by NickDMax: 05 July 2011 - 01:32 PM
Reason for edit:: added spoiler tags...

Was This Post Helpful? 1
  • +
  • -

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