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 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.
-
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
-
Seems like a good professor.
My current favourite word is: Bacon!
-SK Genius
-
Wow, old post, and one voted too. Obviously people don't agree with me :rolleyes: Oh well, i think they seem like a decent proff. anyway, y'all are entitled to your own opinions.
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
Wow, old post, and one voted too. Obviously people don't agree with me :rolleyes: Oh well, i think they seem like a decent proff. anyway, y'all are entitled to your own opinions.
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
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
Stuff like that pisses me off. Might be bad experience, but teachers often feel funny and think they come up with a "good" way of testing the students' knowledge with excessively nested or obscure code. The main reason why it pisses me off is because stuff like that usually never got mentioned before in class. Not even once. Not only that, usually none of the resources mention it either. So the students not only have no reason to ever use something like that, they usually don't even know something like that is even possible. This means they have no idea just what the hell will happen. Thinking it through won't help either because it's not something you can deduct from the stuff you learned, it's only a 50/50 guess whether you get the right answer or not. Might be just my bad experience but I've written too many tests where the larger part was made up of questions like that and the things you really discussed in class and read in the resources made up less than half of it. It was more like guessing the right answers rather than testing the acquired knowledge.