Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Problem taken from a C++ quiz

Problem taken from a C++ quiz

Scheduled Pinned Locked Moved Clever Code
questionc++comhelptutorial
25 Posts 15 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #10

    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

    L 1 Reply Last reply
    0
    • C CPallini

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #11

      Thanks, I just corrected it. Looks like I would have failed the exam this time :-) Sometimes it gets really annoying, how dependant one may get of Intellisense and the likes

      1 Reply Last reply
      0
      • L Lost User

        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

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #12

        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

        L 1 Reply Last reply
        0
        • A Anthony Mushrow

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #13

          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 :-)

          A 1 Reply Last reply
          0
          • L Lost User

            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 :-)

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #14

            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

            L 1 Reply Last reply
            0
            • A Anthony Mushrow

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #15

              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.

              A 1 Reply Last reply
              0
              • L Lost User

                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.

                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #16

                Seems like a good professor.

                My current favourite word is: Bacon!

                -SK Genius

                B 1 Reply Last reply
                0
                • Q QuiJohn

                  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

                  S Offline
                  S Offline
                  Simon Capewell
                  wrote on last edited by
                  #17

                  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:

                  1 Reply Last reply
                  0
                  • J Jay 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

                    V Offline
                    V Offline
                    VentsyV
                    wrote on last edited by
                    #18

                    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 ?

                    M 1 Reply Last reply
                    0
                    • V VentsyV

                      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 ?

                      M Offline
                      M Offline
                      macroideal
                      wrote on last edited by
                      #19

                      it is a new language

                      J 1 Reply Last reply
                      0
                      • M macroideal

                        it is a new language

                        J Offline
                        J Offline
                        Jay Gatsby
                        wrote on last edited by
                        #20

                        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.

                        1 Reply Last reply
                        0
                        • J Jay 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

                          T Offline
                          T Offline
                          tmalbon
                          wrote on last edited by
                          #21

                          insert space between `/' and '*' like this `*px/ *py << std::endl;" it will compile on VC6

                          TMALBONPH

                          1 Reply Last reply
                          0
                          • A Anthony Mushrow

                            Seems like a good professor.

                            My current favourite word is: Bacon!

                            -SK Genius

                            B Offline
                            B Offline
                            bulg
                            wrote on last edited by
                            #22

                            Sounds like an annoying professor

                            A 1 Reply Last reply
                            0
                            • B bulg

                              Sounds like an annoying professor

                              A Offline
                              A Offline
                              Anthony Mushrow
                              wrote on last edited by
                              #23

                              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

                              Game Programming articles start -here[^]-

                              B 1 Reply Last reply
                              0
                              • A Anthony Mushrow

                                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

                                Game Programming articles start -here[^]-

                                B Offline
                                B Offline
                                bulg
                                wrote on last edited by
                                #24

                                woops, didnt see the date [-1 troll] for me >.<

                                1 Reply Last reply
                                0
                                • L Lost User

                                  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

                                  M Offline
                                  M Offline
                                  Megidolaon
                                  wrote on last edited by
                                  #25

                                  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.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups