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. The Weird and The Wonderful
  4. For loops and array

For loops and array

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++data-structures
22 Posts 11 Posters 17 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.
  • M Mladen Jankovic

    for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

    M Offline
    M Offline
    Michael Sadlon
    wrote on last edited by
    #3

    Wow, that is absolutely horrible. I've seen some bad programmers in high school, and this type of code would fit right in there. What a shame ;P

    P D 2 Replies Last reply
    0
    • M Michael Sadlon

      Wow, that is absolutely horrible. I've seen some bad programmers in high school, and this type of code would fit right in there. What a shame ;P

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4

      But in school the teacher would (should) point the offender in the right direction. One of the benefits that schools have over books.

      M 1 Reply Last reply
      0
      • M Michael Sadlon

        Wow, that is absolutely horrible. I've seen some bad programmers in high school, and this type of code would fit right in there. What a shame ;P

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #5

        hah the horrible poker code I saw in HS didn't use arrays or parameter passing. everything was stored in global variables. I probably had as much to do with cluebatting my classmate out of that nonsense as did the teacher. :->

        -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

        1 Reply Last reply
        0
        • P PIEBALDconsult

          But in school the teacher would (should) point the offender in the right direction. One of the benefits that schools have over books.

          M Offline
          M Offline
          Michael Sadlon
          wrote on last edited by
          #6

          Ah, but we DID have an excellent high school teacher. He is still one of my favorite teachers/professors of all time. He was always doing his best to solve people's programming "mistakes" and send them in the right direction. We also had a set of TAs that would help the class learn and code, and they were really helpful. Alas, all the "good" programmers also became TAs by sheer force and there was little you could do to avoid being asked for help five times per class *laughs* In the end, however, some people are beyond hopeless for becoming programmers and should quit while they're not too far behind. I see bad examples of programmers even in college as a Junior (now almost a Senior). I'm amazed how some people made it this far! :laugh:

          1 Reply Last reply
          0
          • M Mladen Jankovic

            for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #7

            Reminds me of an excerpt from a recent article published on CP:

            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square00);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square01);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square02);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square03);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square04);

            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square10);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square11);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square12);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square13);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square14);

            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square20);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square21);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square22);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square23);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square24);

            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square30);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square31);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square32);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square33);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square34);

            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square40);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square41);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square42);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square43);
            if (Convert.ToBoolean(random.Next(2))) ToggleSquare(square44);

            I didn't post a link to the article to protect the innocent(?) :)

            Regards, mav -- Black holes are the places where God divided by 0...

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Oh, my eyes, my eyes! Why 14? What's in e[0]? Is that d[14] a typo?

              M Offline
              M Offline
              Mladen Jankovic
              wrote on last edited by
              #8

              I wish to know that :D

              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)

              1 Reply Last reply
              0
              • M Mladen Jankovic

                for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #9

                WTF! :omg: What else can I say? :doh: I expect from your statement that the amount of code, over all, could be cut in half. I wonder what other coding gaffs where made.

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                M 1 Reply Last reply
                0
                • J John R Shaw

                  WTF! :omg: What else can I say? :doh: I expect from your statement that the amount of code, over all, could be cut in half. I wonder what other coding gaffs where made.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  M Offline
                  M Offline
                  Mladen Jankovic
                  wrote on last edited by
                  #10

                  Same beauty but with the strings :D for (int c = 0; c < b.Length; c++) { if (b[c].Equals("1")) d[1] = d[1] + 1; else if (b[c].Equals("2")) d[2] = d[2] + 1; else if (b[c].Equals("3")) d[3] = d[3] + 1; else if (b[c].Equals("4")) d[4] = d[4] + 1; else if (b[c].Equals("5")) d[5] = d[5] + 1; else if (b[c].Equals("6")) d[6] = d[6] + 1; else if (b[c].Equals("7")) d[7] = d[7] + 1; else if (b[c].Equals("8")) d[8] = d[8] + 1; else if (b[c].Equals("9")) d[9] = d[9] + 1; else if (b[c].Equals("10")) d[10] = d[10] + 1; else if (b[c].Equals("12")) d[12] = d[12] + 1; else if (b[c].Equals("13")) d[13] = d[13] + 1; else if (b[c].Equals("14")) d[14] = d[14] + 1; } and the "d" is: int[] d = new int[15]; As you can see he use indices from 1 to 14 so he hat to set size of array to 15 instead of 14.

                  Mostly, when you see programmers, they aren't doing anything.

                  J K 2 Replies Last reply
                  0
                  • M Mladen Jankovic

                    Same beauty but with the strings :D for (int c = 0; c < b.Length; c++) { if (b[c].Equals("1")) d[1] = d[1] + 1; else if (b[c].Equals("2")) d[2] = d[2] + 1; else if (b[c].Equals("3")) d[3] = d[3] + 1; else if (b[c].Equals("4")) d[4] = d[4] + 1; else if (b[c].Equals("5")) d[5] = d[5] + 1; else if (b[c].Equals("6")) d[6] = d[6] + 1; else if (b[c].Equals("7")) d[7] = d[7] + 1; else if (b[c].Equals("8")) d[8] = d[8] + 1; else if (b[c].Equals("9")) d[9] = d[9] + 1; else if (b[c].Equals("10")) d[10] = d[10] + 1; else if (b[c].Equals("12")) d[12] = d[12] + 1; else if (b[c].Equals("13")) d[13] = d[13] + 1; else if (b[c].Equals("14")) d[14] = d[14] + 1; } and the "d" is: int[] d = new int[15]; As you can see he use indices from 1 to 14 so he hat to set size of array to 15 instead of 14.

                    Mostly, when you see programmers, they aren't doing anything.

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #11

                    Man! If I had the code and the time (which I do not) I would revamp code all over the place. Unrolling loops is an old optimization trick, but you need to know when to do it and when not to. I am not even sure unrolling is even needed today, because the machines today are so fast they make my first computer look like it was standing still. The new I understand, I never do it, but people sometimes do it to make it easier to match values with indexes.

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    1 Reply Last reply
                    0
                    • M Mladen Jankovic

                      Same beauty but with the strings :D for (int c = 0; c < b.Length; c++) { if (b[c].Equals("1")) d[1] = d[1] + 1; else if (b[c].Equals("2")) d[2] = d[2] + 1; else if (b[c].Equals("3")) d[3] = d[3] + 1; else if (b[c].Equals("4")) d[4] = d[4] + 1; else if (b[c].Equals("5")) d[5] = d[5] + 1; else if (b[c].Equals("6")) d[6] = d[6] + 1; else if (b[c].Equals("7")) d[7] = d[7] + 1; else if (b[c].Equals("8")) d[8] = d[8] + 1; else if (b[c].Equals("9")) d[9] = d[9] + 1; else if (b[c].Equals("10")) d[10] = d[10] + 1; else if (b[c].Equals("12")) d[12] = d[12] + 1; else if (b[c].Equals("13")) d[13] = d[13] + 1; else if (b[c].Equals("14")) d[14] = d[14] + 1; } and the "d" is: int[] d = new int[15]; As you can see he use indices from 1 to 14 so he hat to set size of array to 15 instead of 14.

                      Mostly, when you see programmers, they aren't doing anything.

                      K Offline
                      K Offline
                      Kochise
                      wrote on last edited by
                      #12

                      This stuff is stunning, not even a line of comment ! Kochise

                      In Code we trust !

                      P 1 Reply Last reply
                      0
                      • K Kochise

                        This stuff is stunning, not even a line of comment ! Kochise

                        In Code we trust !

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #13

                        Oh man, I didn't even notice the absence of 11!

                        M 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Oh man, I didn't even notice the absence of 11!

                          M Offline
                          M Offline
                          Mladen Jankovic
                          wrote on last edited by
                          #14

                          Cards 1 and 11 are the same, so 11 is skipped, at least something is right ;)

                          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)

                          P 1 Reply Last reply
                          0
                          • M Mladen Jankovic

                            Cards 1 and 11 are the same, so 11 is skipped, at least something is right ;)

                            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)

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #15

                            So the underlying design is flawed as well?

                            D 1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              So the underlying design is flawed as well?

                              D Offline
                              D Offline
                              Dan Neely
                              wrote on last edited by
                              #16

                              Could be depending on the rules of the game. Normally ace is the highest and lowest card, if for some reason an ace could be a 1, or between 10 and jack the implementation would sorta make sense.

                              -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                              P 1 Reply Last reply
                              0
                              • D Dan Neely

                                Could be depending on the rules of the game. Normally ace is the highest and lowest card, if for some reason an ace could be a 1, or between 10 and jack the implementation would sorta make sense.

                                -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #17

                                dan neely wrote:

                                would sorta make sense.

                                ...but not much, there's probably a cleaner way.

                                D 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  dan neely wrote:

                                  would sorta make sense.

                                  ...but not much, there's probably a cleaner way.

                                  D Offline
                                  D Offline
                                  Dan Neely
                                  wrote on last edited by
                                  #18

                                  PIEBALDconsult wrote:

                                  dan neely wrote: would sorta make sense. ...but not much, there's probably a cleaner way.

                                  the sorta was representing that fact. I didn't immediately see a better way, but assumed a less ugly construct still should exist.

                                  -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    Oh, my eyes, my eyes! Why 14? What's in e[0]? Is that d[14] a typo?

                                    J Offline
                                    J Offline
                                    jhwurmbach
                                    wrote on last edited by
                                    #19

                                    PIEBALDconsult wrote:

                                    What's in e[0]?

                                    e[0]? Whats e[0]? Indices are starting from one, ever since VB existed! If you are really oldscool, you may even substitute VB with Fortran...


                                    Failure is not an option - it's built right in.

                                    1 Reply Last reply
                                    0
                                    • M Mladen Jankovic

                                      for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

                                      B Offline
                                      B Offline
                                      Brent Lamborn
                                      wrote on last edited by
                                      #20

                                      LMFAO...that's funny. Thanks for the laugh. I'm such a geek..laughing about code.:cool:


                                      "Half this game is ninety percent mental." - Yogi Berra If you can read thank a teacher, if you can read in English, thank a Marine.

                                      1 Reply Last reply
                                      0
                                      • M Mladen Jankovic

                                        for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

                                        D Offline
                                        D Offline
                                        D111
                                        wrote on last edited by
                                        #21

                                        whats so difficult about: for (int c = 0; c < _myHand.CardCount; c++) { e[_myHand.Cards[c]]++; } This might not work, depending on how the value of the cards are stored, though, in which case just cast to int

                                        --- The sum of the intelligence of the world is constant. The total number of people is always increasing.

                                        1 Reply Last reply
                                        0
                                        • M Mladen Jankovic

                                          for (int c = 0; c < _myHand.CardCount; c++) { if (_myHand.Cards[c].Equals(1)) e[1] = e[1] + 1; else if (_myHand.Cards[c].Equals(2)) e[2] = e[2] + 1; else if (_myHand.Cards[c].Equals(3)) e[3] = e[3] + 1; else if (_myHand.Cards[c].Equals(4)) e[4] = e[4] + 1; else if (_myHand.Cards[c].Equals(5)) e[5] = e[5] + 1; else if (_myHand.Cards[c].Equals(6)) e[6] = e[6] + 1; else if (_myHand.Cards[c].Equals(7)) e[7] = e[7] + 1; else if (_myHand.Cards[c].Equals(8)) e[8] = e[8] + 1; else if (_myHand.Cards[c].Equals(9)) e[9] = e[9] + 1; else if (_myHand.Cards[c].Equals(10)) e[10] = e[10] + 1; else if (_myHand.Cards[c].Equals(12)) e[12] = e[12] + 1

                                          S Offline
                                          S Offline
                                          Sylvester george
                                          wrote on last edited by
                                          #22

                                          Oh My God, Please visit this Great Horror

                                          Regards, Sylvester G sylvester_g_m@yahoo.com

                                          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