Problem taken from a C++ quiz
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
:omg: Great! Thanks :)
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
I don't want to ruin it if it isn't this...
Jay Gatsby wrote:
/*py << std::endl; return 0; }
... is it because the /* before py is interpreted as the start of a block comment?
-
I don't want to ruin it if it isn't this...
Jay Gatsby wrote:
/*py << std::endl; return 0; }
... is it because the /* before py is interpreted as the start of a block comment?
Indeed, the resulting syntax error is the answer. Does it compile? No.
-Gatsby
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
/*
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist -
Bah, I would've gotten it right if I had pasted it into my color coded editor. :) I used to feel like that was cheating, now I depend on it.
Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency! -Emily Dickinson
Now you know how I feel when I'm left without Visual Assist...
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist -
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
This brings up memories from first and second semester programming courses, like this:
int x = 5; int y = 100; int z = 0; int* px = &x; int* py= &y; z = *px**py;
Would this execute properly? If yes, what is the value of z after execution?modified on Wednesday, April 23, 2008 10:14 AM
-
This brings up memories from first and second semester programming courses, like this:
int x = 5; int y = 100; int z = 0; int* px = &x; int* py= &y; z = *px**py;
Would this execute properly? If yes, what is the value of z after execution?modified on Wednesday, April 23, 2008 10:14 AM
CDP1802 wrote:
Would this execute properly?
Who knows: it doesn't compile (i.e. did you intend
z = *px**py
?).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
CDP1802 wrote:
Would this execute properly?
Who knows: it doesn't compile (i.e. did you intend
z = *px**py
?).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
This brings up memories from first and second semester programming courses, like this:
int x = 5; int y = 100; int z = 0; int* px = &x; int* py= &y; z = *px**py;
Would this execute properly? If yes, what is the value of z after execution?modified on Wednesday, April 23, 2008 10:14 AM
Would that even compile? I'm going to say it wouldn't work out. And then go and check... EDIT:I guess the compiler isn't an idiot, it works just fine.
My current favourite word is: Bacon!
-SK Genius
-
Would that even compile? I'm going to say it wouldn't work out. And then go and check... EDIT:I guess the compiler isn't an idiot, it works just fine.
My current favourite word is: Bacon!
-SK Genius
-
Its not really hard to figure out and can be done to eternity with other pointer types:
int x = 5; int y = 100; int z = 0; int* px = &x; int* py= &y; int** ppx = &px; int** ppy = &py; z = **ppx***ppy;
The result is exactly the same as before :-)I just though perhaps the compiler would get confused and start going on about illegal indirection or whatever it is. Still, i wouldn't put it into any of my projects, not like that anyway, i'd probably add brackets to avoid confusion.
z = (*px)*(*py);
My current favourite word is: Bacon!
-SK Genius
-
I just though perhaps the compiler would get confused and start going on about illegal indirection or whatever it is. Still, i wouldn't put it into any of my projects, not like that anyway, i'd probably add brackets to avoid confusion.
z = (*px)*(*py);
My current favourite word is: Bacon!
-SK Genius
-
Very true. The professor included things like this into his exams to see if you really understood how such lines were interpreted. He did not recommend such things as good examples of well-written code.
Seems like a good professor.
My current favourite word is: Bacon!
-SK Genius
-
Bah, I would've gotten it right if I had pasted it into my color coded editor. :) I used to feel like that was cheating, now I depend on it.
Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency! -Emily Dickinson
You wouldn't if you were they guy I used to work with. He spent an entire afternoon scratching his head over why the (abysmal) code he'd just written wasn't having any effect. He called a few people over to help and, surprise surprise, the entire screen was filled with green text :rolleyes:
-
This isn't the exact question (I can't remember what it was exactly) but this example keeps to the spirit of the problem. There was a series of questions which had subtle bugs in them, which all had to be answered in the following way: Does it compile? If so, does it run without (potentially) crashing? If so, what does it print out?
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
When you think you know the answer, go to http://pastebin.com/f57900ce I actually got this one right.-Gatsby
-
Jay Gatsby wrote:
#include // for cout int main() { int x = 4; int y = 2; int *px = &x; int *py = &y; std::cout << "4 / 2 = " << *px/*py << std::endl; return 0; }
#include // for cout - is that somekind of new header file I have not heard about ?
it is a new language
-
it is a new language
no, that's me forgetting to click the ignore HTML check-box when I made the post, therefore losing . Everyone else seemed to understand that just fine.