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. General Programming
  3. C / C++ / MFC
  4. Loop thru two dimensional array - edited

Loop thru two dimensional array - edited

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondatabasehardware
8 Posts 3 Posters 21 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 Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Minor add: I think the loop increment is wrong I have commented out inner loop and addend two more list to the array so the decimal count should be 5. OK the code say Array size 4 and it misses the last array. That should be a snap to fix...

    <"TASK #ifdef FUNCTION_TEST_PROJECT int MainWindow_Bluetooth::CreateMenus_Array()79"
    " index array 0 Array size 4"
    " index array 1 Array size 4"
    " index array 2 Array size 4"
    " index array 3 Array size 4"/pre>

    OK, I have plain C question about nested "loop"

    My task is to loop thru two dimensional array...
    The error in my code is stopping the embedded loop.

    Here is a snippet of the run debug messages

    here is the "problem " code

    for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
    What am I doing wrong in sizing the array?
    Many thanks for help resolving this.

    Debug

    " index array 0Array size 4"
    " main menus 0 SubWindow terminal (QMdiArea )main menu size 34"
    " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
    " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
    " main menus 3 SubWindow system (QMdiArea )main menu size 34"
    ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
    12:23:02: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.

    Array;

    const QStringList list_array[10]
    {
    { "SubWindow terminal (QMdiArea )",
    "SubWindow hcitool (QMdiArea )",
    "SubWindow bluetoothctl (QMdiArea )",
    "SubWindow system (QMdiArea )" },

        { "SubsubWindow bluetoothctl help        (QMdiArea )",
            "SubsubWindow bluetoothctl list  (QMdiArea )",
            "SubsubWindow bluetoothctl devices (QMdiArea )",
            "SubsubWindow bluetoothctl show  (QMdiArea )" },
    
        { "SubsubWindow hcitool        (QMdiArea )",
            "SubsubWindow hcitool   dev  (QMdiArea )",
            "SubsubWindow hcitool   scan (QMdiArea )",
            "SubsubWindow hcitool   inq  (QMdiArea )"}
    };
    

    Code:

    for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )
    {
    text = " index array ";
    text += QString::number(index_array);
    text += "Array size ";
    text += QString::number(list_array[index_arra

    J K L 3 Replies Last reply
    0
    • L Lost User

      Minor add: I think the loop increment is wrong I have commented out inner loop and addend two more list to the array so the decimal count should be 5. OK the code say Array size 4 and it misses the last array. That should be a snap to fix...

      <"TASK #ifdef FUNCTION_TEST_PROJECT int MainWindow_Bluetooth::CreateMenus_Array()79"
      " index array 0 Array size 4"
      " index array 1 Array size 4"
      " index array 2 Array size 4"
      " index array 3 Array size 4"/pre>

      OK, I have plain C question about nested "loop"

      My task is to loop thru two dimensional array...
      The error in my code is stopping the embedded loop.

      Here is a snippet of the run debug messages

      here is the "problem " code

      for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
      What am I doing wrong in sizing the array?
      Many thanks for help resolving this.

      Debug

      " index array 0Array size 4"
      " main menus 0 SubWindow terminal (QMdiArea )main menu size 34"
      " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
      " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
      " main menus 3 SubWindow system (QMdiArea )main menu size 34"
      ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
      12:23:02: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.

      Array;

      const QStringList list_array[10]
      {
      { "SubWindow terminal (QMdiArea )",
      "SubWindow hcitool (QMdiArea )",
      "SubWindow bluetoothctl (QMdiArea )",
      "SubWindow system (QMdiArea )" },

          { "SubsubWindow bluetoothctl help        (QMdiArea )",
              "SubsubWindow bluetoothctl list  (QMdiArea )",
              "SubsubWindow bluetoothctl devices (QMdiArea )",
              "SubsubWindow bluetoothctl show  (QMdiArea )" },
      
          { "SubsubWindow hcitool        (QMdiArea )",
              "SubsubWindow hcitool   dev  (QMdiArea )",
              "SubsubWindow hcitool   scan (QMdiArea )",
              "SubsubWindow hcitool   inq  (QMdiArea )"}
      };
      

      Code:

      for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )
      {
      text = " index array ";
      text += QString::number(index_array);
      text += "Array size ";
      text += QString::number(list_array[index_arra

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      I've never used Qt before but, this line seems odd,

      for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )

      specifically

      index_array < list_array[index_array].size()

      Maybe try something like

      for (index_array = 0; index_array < list_array.size(); ++index_array )

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

      L 1 Reply Last reply
      0
      • J jeron1

        I've never used Qt before but, this line seems odd,

        for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )

        specifically

        index_array < list_array[index_array].size()

        Maybe try something like

        for (index_array = 0; index_array < list_array.size(); ++index_array )

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

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

        Sorry, no go. This is not about Qt - I no longer post questions about Qt - this is wrong forum for Qt. This is plain C question - hence in "C..." sub forum. In my view each loop has its stopping point. The first - parent loop - needs to stop when it reaches the end AND its "size" is correct (see debug message ) and I can run it bypassing the "inner " loop. The size of inner loop is wrong - see debug messages posted. Changing the parent loop size as suggested gets this error:

        /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth.cpp:142: error: member reference base type 'const QStringList[10]' is not a structure or union
        mainwindow_Bluetooth.cpp:142:52: error: member reference base type 'const QStringList[10]' is not a structure or union
        for (index_array = 0; index_array < list_array.size(); ++index_array )
        ~~~~~~^

        1 Reply Last reply
        0
        • L Lost User

          Minor add: I think the loop increment is wrong I have commented out inner loop and addend two more list to the array so the decimal count should be 5. OK the code say Array size 4 and it misses the last array. That should be a snap to fix...

          <"TASK #ifdef FUNCTION_TEST_PROJECT int MainWindow_Bluetooth::CreateMenus_Array()79"
          " index array 0 Array size 4"
          " index array 1 Array size 4"
          " index array 2 Array size 4"
          " index array 3 Array size 4"/pre>

          OK, I have plain C question about nested "loop"

          My task is to loop thru two dimensional array...
          The error in my code is stopping the embedded loop.

          Here is a snippet of the run debug messages

          here is the "problem " code

          for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
          What am I doing wrong in sizing the array?
          Many thanks for help resolving this.

          Debug

          " index array 0Array size 4"
          " main menus 0 SubWindow terminal (QMdiArea )main menu size 34"
          " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
          " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
          " main menus 3 SubWindow system (QMdiArea )main menu size 34"
          ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
          12:23:02: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.

          Array;

          const QStringList list_array[10]
          {
          { "SubWindow terminal (QMdiArea )",
          "SubWindow hcitool (QMdiArea )",
          "SubWindow bluetoothctl (QMdiArea )",
          "SubWindow system (QMdiArea )" },

              { "SubsubWindow bluetoothctl help        (QMdiArea )",
                  "SubsubWindow bluetoothctl list  (QMdiArea )",
                  "SubsubWindow bluetoothctl devices (QMdiArea )",
                  "SubsubWindow bluetoothctl show  (QMdiArea )" },
          
              { "SubsubWindow hcitool        (QMdiArea )",
                  "SubsubWindow hcitool   dev  (QMdiArea )",
                  "SubsubWindow hcitool   scan (QMdiArea )",
                  "SubsubWindow hcitool   inq  (QMdiArea )"}
          };
          

          Code:

          for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )
          {
          text = " index array ";
          text += QString::number(index_array);
          text += "Array size ";
          text += QString::number(list_array[index_arra

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          I think you've got your logic in a twist here. I think it should be

          for(index_array = 0; index_array < list_array.size(); ++index_array )
          {
          // stuff
          for(index_main = 0; index_main < list_array[index_array].size(); ++index_main )
          {
          // more stuff
          }
          }

          Additionally, note that you're calculating list_array.size() and list_array[index_array].size() multiple times. Perhaps better would be

          const size_t list_max = list_array.size();
          for( index_array = 0; index_array < list_max; ++index_array )
          {
          // stuff
          const size_t array_max = list_array[index_array].size();
          for(index_main = 0; index_main < array_max; ++index_main)
          {
          //more stuff
          }

          Maybe, since you've declared list_array as const, the compiler will be able to optimize away the unneeded function calls, but speaking for myself, I find my version just a little easier to read and understand.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          L 1 Reply Last reply
          0
          • K k5054

            I think you've got your logic in a twist here. I think it should be

            for(index_array = 0; index_array < list_array.size(); ++index_array )
            {
            // stuff
            for(index_main = 0; index_main < list_array[index_array].size(); ++index_main )
            {
            // more stuff
            }
            }

            Additionally, note that you're calculating list_array.size() and list_array[index_array].size() multiple times. Perhaps better would be

            const size_t list_max = list_array.size();
            for( index_array = 0; index_array < list_max; ++index_array )
            {
            // stuff
            const size_t array_max = list_array[index_array].size();
            for(index_main = 0; index_main < array_max; ++index_main)
            {
            //more stuff
            }

            Maybe, since you've declared list_array as const, the compiler will be able to optimize away the unneeded function calls, but speaking for myself, I find my version just a little easier to read and understand.

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

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

            Sorry, but the compiler till complain about this - without specifying it is an arraray as [10]

            /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth.cpp:168: error: member reference base type 'const QStringList[10]' is not a structure or union
            mainwindow_Bluetooth.cpp:168:39: error: member reference base type 'const QStringList[10]' is not a structure or union
            const size_t list_max = list_array.size();
            ~~~~~~^

            const size_t list_max = list_array.size();
            for( index_array = 0; index_array < list_max; ++index_array )
            {
            // stuff
            const size_t array_max = list_array[index_array].size();
            for(index_main = 0; index_main < array_max; ++index_main)
            {
            //more stuff
            }
            }

            1 Reply Last reply
            0
            • L Lost User

              Minor add: I think the loop increment is wrong I have commented out inner loop and addend two more list to the array so the decimal count should be 5. OK the code say Array size 4 and it misses the last array. That should be a snap to fix...

              <"TASK #ifdef FUNCTION_TEST_PROJECT int MainWindow_Bluetooth::CreateMenus_Array()79"
              " index array 0 Array size 4"
              " index array 1 Array size 4"
              " index array 2 Array size 4"
              " index array 3 Array size 4"/pre>

              OK, I have plain C question about nested "loop"

              My task is to loop thru two dimensional array...
              The error in my code is stopping the embedded loop.

              Here is a snippet of the run debug messages

              here is the "problem " code

              for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
              What am I doing wrong in sizing the array?
              Many thanks for help resolving this.

              Debug

              " index array 0Array size 4"
              " main menus 0 SubWindow terminal (QMdiArea )main menu size 34"
              " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
              " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
              " main menus 3 SubWindow system (QMdiArea )main menu size 34"
              ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
              12:23:02: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.

              Array;

              const QStringList list_array[10]
              {
              { "SubWindow terminal (QMdiArea )",
              "SubWindow hcitool (QMdiArea )",
              "SubWindow bluetoothctl (QMdiArea )",
              "SubWindow system (QMdiArea )" },

                  { "SubsubWindow bluetoothctl help        (QMdiArea )",
                      "SubsubWindow bluetoothctl list  (QMdiArea )",
                      "SubsubWindow bluetoothctl devices (QMdiArea )",
                      "SubsubWindow bluetoothctl show  (QMdiArea )" },
              
                  { "SubsubWindow hcitool        (QMdiArea )",
                      "SubsubWindow hcitool   dev  (QMdiArea )",
                      "SubsubWindow hcitool   scan (QMdiArea )",
                      "SubsubWindow hcitool   inq  (QMdiArea )"}
              };
              

              Code:

              for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )
              {
              text = " index array ";
              text += QString::number(index_array);
              text += "Array size ";
              text += QString::number(list_array[index_arra

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

              SOLVED Wrong logic in embedded loop - this is size of CURRENT / dynamic MAIN loop member. Thanks very much for all the help. Sal

              for (index\_array = 0; index\_array <  list\_array\[index\_array\].size()+1; ++index\_array  )
              {
                  text = " index array ";
                  text += QString::number(index\_array);
                  text += "  Array size ";
                  text += QString::number(list\_array\[index\_array\].size() );
                  qDebug() << text;
              
              for (index\_main = 0; index\_main < **list\_array\[index\_array\].size();** ++index\_main)
              {
                  text = " main menus  ";
                  text += QString::number(index\_main);
                  text += " ";
                  text += list\_array\[index\_array\]\[index\_main\];
                  text += "main menu size ";
                  text += QString::number(list\_array\[index\_array\]\[index\_main\].size());
                  qDebug() << text;
              }
              }
              
              return 0;
              
              {// menus processing
              

              Found the cause of the weird count The code counts # of characters in the member of the child array.... " index array 0 Array size 7" " main menus 0 SubWindow terminal added space here (QMdiArea )main menu size 57" " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34" " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34" " main menus 3 SubWindow terminal (QMdiArea )main menu size 34" " main menus 4 SubWindow hcitool (QMdiArea )main menu size 34" " main menus 5 SubWindow bluetoothctl (QMdiArea )main menu size 34" " main menus 6 SubWindow system (QMdiArea )main menu size 34" ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575

              J 1 Reply Last reply
              0
              • L Lost User

                SOLVED Wrong logic in embedded loop - this is size of CURRENT / dynamic MAIN loop member. Thanks very much for all the help. Sal

                for (index\_array = 0; index\_array <  list\_array\[index\_array\].size()+1; ++index\_array  )
                {
                    text = " index array ";
                    text += QString::number(index\_array);
                    text += "  Array size ";
                    text += QString::number(list\_array\[index\_array\].size() );
                    qDebug() << text;
                
                for (index\_main = 0; index\_main < **list\_array\[index\_array\].size();** ++index\_main)
                {
                    text = " main menus  ";
                    text += QString::number(index\_main);
                    text += " ";
                    text += list\_array\[index\_array\]\[index\_main\];
                    text += "main menu size ";
                    text += QString::number(list\_array\[index\_array\]\[index\_main\].size());
                    qDebug() << text;
                }
                }
                
                return 0;
                
                {// menus processing
                

                Found the cause of the weird count The code counts # of characters in the member of the child array.... " index array 0 Array size 7" " main menus 0 SubWindow terminal added space here (QMdiArea )main menu size 57" " main menus 1 SubWindow hcitool (QMdiArea )main menu size 34" " main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34" " main menus 3 SubWindow terminal (QMdiArea )main menu size 34" " main menus 4 SubWindow hcitool (QMdiArea )main menu size 34" " main menus 5 SubWindow bluetoothctl (QMdiArea )main menu size 34" " main menus 6 SubWindow system (QMdiArea )main menu size 34" ASSERT failure in QList::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #7

                To me anyway, your for loop test seems tailored to the actual counts of items and subitems, what if each QStringList had 15 strings instead of 4? Couldn't you use containers analogous to stl contianers something along the lines of;

                QList myList;

                QStringList strlist1 = { "Blah1", "Blah2", "Blah3" };
                QStringList strlist2 = { "Blah4", "Blah5", "Blah6" };
                QStringList strlist3 = { "Blah7", "Blah8", "Blah9" };

                myList.push_back(strList1);
                myList.push_back(strList2);
                myList.push_back(strList3);

                for (int mainIndex = 0; mainIndex < myList.size(); mainIndex++)
                {
                // do item stuff

                 for (int subIndex = 0; subIndex < myList\[mainIndex\].size(); subIndex++)
                 {
                    // do some sub item stuff
                 }
                

                }

                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                L 1 Reply Last reply
                0
                • J jeron1

                  To me anyway, your for loop test seems tailored to the actual counts of items and subitems, what if each QStringList had 15 strings instead of 4? Couldn't you use containers analogous to stl contianers something along the lines of;

                  QList myList;

                  QStringList strlist1 = { "Blah1", "Blah2", "Blah3" };
                  QStringList strlist2 = { "Blah4", "Blah5", "Blah6" };
                  QStringList strlist3 = { "Blah7", "Blah8", "Blah9" };

                  myList.push_back(strList1);
                  myList.push_back(strList2);
                  myList.push_back(strList3);

                  for (int mainIndex = 0; mainIndex < myList.size(); mainIndex++)
                  {
                  // do item stuff

                   for (int subIndex = 0; subIndex < myList\[mainIndex\].size(); subIndex++)
                   {
                      // do some sub item stuff
                   }
                  

                  }

                  "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

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

                  Yes, pretty much similar code. ..and the idea of "variable count ", I will eventually have a file where I will keep the data and will add to it as required - sort of database . I messed up a little and had the main loop in the array... It has been great "discussion" and it helped a lot... Thanks PS I may post the final version ,if anybody cares. Right now it is lot of "cut and paste" to put it in code which actuality does puts menu /submenu into main code.

                  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