C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
give me some logical program Posted by sanjay_kkoo on 3 Nov 2006 at 6:08 AM

Report
Re: give me some logical program Posted by Lundin on 3 Nov 2006 at 6:42 AM
Alright, here you go. Why doesn't this code work?
No matter which number you enter, the program will
always treat it as the number 5. Why?

#include <stdio.h>
#include <math.h>

int main()
{
  int x;
  
  printf("Enter any value but 5: ");
  scanf("%d", &x);
  getchar();

  x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;

  if(x=!5)  
    printf("You didn't type '5'! Congratulations.\n");
  else
    printf("Bleh, you typed '5'.\n");
    
  return 0;
}


Report
Re: give me some logical program Posted by IDK on 3 Nov 2006 at 11:23 AM
:   x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;


Hmm, if I'm not wrong, shouldn't there be a = after the >> or something?

And it seems to alwasy crash for me... Does it need to be compiled as a dos project or something to run? or is it just my compiler (could be becouse it's bad).
Report
Re: give me some logical program Posted by stober on 3 Nov 2006 at 6:08 PM
:
: :   x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;
: 

:
: Hmm, if I'm not wrong, shouldn't there be a = after the >> or something?
:
: And it seems to alwasy crash for me... Does it need to be compiled as a dos project or something to run? or is it just my compiler (could be becouse it's bad).
:


It crashes in MS-Windows 32-bit compiler because its attempting to access memory it does not own.
Report
Re: give me some logical program Posted by Lundin on 6 Nov 2006 at 12:58 AM
: :
: : :   x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;
: : 

: :
: : Hmm, if I'm not wrong, shouldn't there be a = after the >> or something?
: :
: : And it seems to alwasy crash for me... Does it need to be compiled as a dos project or something to run? or is it just my compiler (could be becouse it's bad).
: :
:
:
: It crashes in MS-Windows 32-bit compiler because its attempting to access memory it does not own.
:


Yeah probably. But from a platform-independant point of view, it shouldn't. The solution is actually very simple, you don't even need to run it to find it. And nope, there shouldn't be a = after >>
Report
Re: give me some logical program Posted by IDK on 6 Nov 2006 at 5:38 AM
:
: Yeah probably. But from a platform-independant point of view, it shouldn't. The solution is actually very simple, you don't even need to run it to find it. And nope, there shouldn't be a = after >>
:

OK, I was a little confused there, becouse it looked very wrong...
I fixed your typing errors, but they where the sollution....

With some equation solving:
x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;
x >> (*(int*)(int)(.76) ^ 0xFF00 % 33) % 8;
x >> (*(int*)1 ^ 0xFF00 % 33) % 8;


Then it needs to get what comes after NULL, which should make every program crash. How does it run on every platform then? (if the compiler doesn't optimise away the statement)
Report
Re: give me some logical program Posted by Lundin on 6 Nov 2006 at 7:48 AM
: :
: : Yeah probably. But from a platform-independant point of view, it shouldn't. The solution is actually very simple, you don't even need to run it to find it. And nope, there shouldn't be a = after >>
: :
:
: OK, I was a little confused there, becouse it looked very wrong...
: I fixed your typing errors, but they where the sollution....
:
: With some equation solving:
:
: x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;
: x >> (*(int*)(int)(.76) ^ 0xFF00 % 33) % 8;
: x >> (*(int*)1 ^ 0xFF00 % 33) % 8;
: 

:
: Then it needs to get what comes after NULL, which should make every program crash. How does it run on every platform then? (if the compiler doesn't optimise away the statement)
:


Heh, you are going to be pissed off when I tell you the solution.
It is a tricky question: I already gave the hint that x >> was not a typo...
Report
Re: give me some logical program Posted by IDK on 6 Nov 2006 at 3:08 PM
: : :
: : : Yeah probably. But from a platform-independant point of view, it shouldn't. The solution is actually very simple, you don't even need to run it to find it. And nope, there shouldn't be a = after >>
: : :
: :
: : OK, I was a little confused there, becouse it looked very wrong...
: : I fixed your typing errors, but they where the sollution....
: :
: : With some equation solving:
: :
: : x >> (*(int*)(int)(43.54*sin(3.14/180.0)) ^ 0xFF00 % 33) % 8;
: : x >> (*(int*)(int)(.76) ^ 0xFF00 % 33) % 8;
: : x >> (*(int*)1 ^ 0xFF00 % 33) % 8;
: : 

: :
: : Then it needs to get what comes after NULL, which should make every program crash. How does it run on every platform then? (if the compiler doesn't optimise away the statement)
: :
:
:
: Heh, you are going to be pissed off when I tell you the solution.
: It is a tricky question: I already gave the hint that x >> was not a typo...
:

I've already found the sullotion, but it still crashes.
(*(int*)1) will be segmentation fault on all unixes, and the code is therefor NOT platformindependent.
Report
Re: give me some logical program Posted by stober on 6 Nov 2006 at 5:54 PM
:
: I've already found the sullotion, but it still crashes.
: (*(int*)1) will be segmentation fault on all unixes, and the code is therefor NOT platformindependent.
:

I agree -- very similar to this code, which will also crash
int x = *(int *)0;


Report
Re: give me some logical program Posted by Lundin on 7 Nov 2006 at 12:12 AM
: :
: : I've already found the sullotion, but it still crashes.
: : (*(int*)1) will be segmentation fault on all unixes, and the code is therefor NOT platformindependent.
: :
:
: I agree -- very similar to this code, which will also crash
:
: int x = *(int *)0;
: 

:
:


Alright, I agree, you found one problem but not the solution.

No matter which number you enter, the program will
always treat it as the number 5. Why?

Report
Re: give me some logical program Posted by IDK on 7 Nov 2006 at 9:29 AM
: : :
: : : I've already found the sullotion, but it still crashes.
: : : (*(int*)1) will be segmentation fault on all unixes, and the code is therefor NOT platformindependent.
: : :
: :
: : I agree -- very similar to this code, which will also crash
: :
: : int x = *(int *)0;
: : 

: :
: :
:
:
: Alright, I agree, you found one problem but not the solution.
:
: No matter which number you enter, the program will
: always treat it as the number 5. Why?

:

I didn't want to give away the solution to the OP.

But anyway here it is:
  if(x=!5)  
    printf("You didn't type '5'! Congratulations.\n");

The condition will never return true, becouse !5 is 0 and x=0 is 0, which is false.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.