Printing combinations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • capablanca
    New Member
    • May 2010
    • 60

    Printing combinations

    Hello: C Sharp, C++
    My question is:
    Is it possible to print two string arrays with different lengths inside of the same for()loops, i tried to do, but so far without any positive result. Thanks
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What have you tried so far?

    Comment

    • capablanca
      New Member
      • May 2010
      • 60

      #3
      Thanks Rabbit for being interested in helping me, well the question i posted is because i created a program that does the following:

      1) Print a first string array using 5 for() loops
      2) Print a second string array using 5 for() loops
      3) Print the equals rows between both arrays

      The program runs fine, but its running time is about 8 minutes, is for this reason i posted my question to get a way to save running time and gain a better performance. So far i tried some ideas but without a positive result.
      If you can help me i really appreciate it, thanks.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Please show your work.
        Format your code by selecting it and then using the [CODE/] format button.

        Comment

        • capablanca
          New Member
          • May 2010
          • 60

          #5
          Code:
          This is the code:
          
           // Print string[] firstArray.
                      for (int i = 0; i < firstArray.Length - 4; i++)
                      {
                          for (int j = i + 1; j < firstArray.Length - 3; j++)
                          {
                              for (int k = j + 1; k < firstArray.Length - 2; k++)
                              {
                                  for (int l = k + 1; l < firstArray.Length - 1; l++)
                                  {
                                      for (int o = l + 1; o < firstArray.Length; o++)
                                      {
                                          dataGridView1.Rows.Add();
                                          ++combinationCounterFirstArray;
          
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[0].Value = combinationCounterFirstArray;
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[1].Value = firstArray[i];
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[2].Value = firstArray[j];
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[3].Value = firstArray[k];
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[4].Value = firstArray[l];
                                          dataGridView1.Rows[combinationCounterFirstArray - 1].Cells[5].Value = firstArray[o];   
           
                                          primerArray = primerArray + firstArray[i] + " " + firstArray[j] + " " + firstArray[k] + " " +
                                                        firstArray[l] + " " + firstArray[o] + " ";
                                      }
                                  }
                              }
                          }
                      }
               
                      // Print string[] secondArray.
                      for (int i = 0; i < secondArray.Length - 4; i++)
                      {
                          for (int j = i + 1; j < secondArray.Length - 3; j++)
                          {
                              for (int k = j + 1; k < secondArray.Length - 2; k++)
                              {
                                  for (int l = k + 1; l < secondArray.Length - 1; l++)
                                  {
                                      for (int o = l + 1; o < secondArray.Length; o++)
                                      {
                                          dataGridView2.Rows.Add();
                                          ++combinationCounterSecondArray;
          
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[0].Value = combinationCounterSecondArray;
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[1].Value = secondArray[i];
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[2].Value = secondArray[j];
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[3].Value = secondArray[k];
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[4].Value = secondArray[l];
                                          dataGridView2.Rows[combinationCounterSecondArray - 1].Cells[5].Value = secondArray[o];  
          
                                          segundoArray = segundoArray + secondArray[i] + " " + secondArray[j] + " " + secondArray[k] + " " +
                                                         secondArray[l] + " " + secondArray[o] + " ";
                                      }
                                  }
                              }
                          }
                      }
                  
                      int[,] myFirstArray = new int[combinationCounterFirstArray, 5];
          
                      for (int i = 0; i < combinationCounterFirstArray; i++)
                      {
                          string[] myStringArray = primerArray.Split(' ');
          
                          for (int j = 0; j < 5; j++)
                          {
                              myFirstArray[i, j] = Convert.ToInt32(myStringArray[(i * 5) + j]);
                          }
                      }  
          
                      int[,] mySecondArray = new int[combinationCounterSecondArray, 5];
          
                      for (int i = 0; i < combinationCounterSecondArray; i++)
                      {
                          string[] myStringArray = segundoArray.Split(' ');
          
                          for (int j = 0; j < 5; j++)
                          {
                              mySecondArray[i, j] = Convert.ToInt32(myStringArray[(i * 5) + j]);
                          }
                      }
                   
                      // Print the rows that both arrays have equal
                      if (combinationCounterFirstArray <= combinationCounterSecondArray)
                      {
                          for (int i = 0; i < combinationCounterFirstArray; i++)
                          {
                              bool rowUnique = true;
          
                              for (int j = 0; j < combinationCounterSecondArray; j++)
                              {
                                  if (compareTwoDimensionalArrays(ref myFirstArray, i, ref mySecondArray, j, ref columns) == true)
                                  {
                                      rowUnique = false;
                                      break;
                                  }
                              }
                              if (!rowUnique)
                              {
                                  dataGridView3.Rows.Add();
          
                                  for (int k = 0; k < 5; k++)
                                  {
                                      dataGridView3.Rows[rowUniqueCounter].Cells[0].Value = rowUniqueCounter + 1;
                                      dataGridView3.Rows[rowUniqueCounter].Cells[k + 1].Value = myFirstArray[i, k];
          
                                      recordCombinations = recordCombinations + rowUniqueCounter + 1 + " " + myFirstArray[i, k] + " ";
                                  }
                                  rowUniqueCounter++;
                              }
                          }
                      }
                      else
                      {
                          for (int i = 0; i < combinationCounterSecondArray; i++)
                          {
                              bool rowUnique = true;
          
                              for (int j = 0; j < combinationCounterFirstArray; j++)
                              {
                                  if (compareTwoDimensionalArrays(ref mySecondArray, i, ref myFirstArray, j, ref columns) == true)
                                  {
                                      rowUnique = false;
                                      break;
                                  }
                              }
                              if (!rowUnique)
                              {
                                  dataGridView3.Rows.Add();
          
                                  for (int k = 0; k < 5; k++)
                                  {
                                      dataGridView3.Rows[rowUniqueCounter].Cells[0].Value = rowUniqueCounter + 1;
                                      dataGridView3.Rows[rowUniqueCounter].Cells[k + 1].Value = mySecondArray[i, k];
          
                                      recordCombinations = recordCombinations + rowUniqueCounter + 1 + " " + mySecondArray[i, k] + " ";
                                  }
                                  rowUniqueCounter++;
                              }
                          }
                      }

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Why do you have so many loops? What is the output that you're trying to create?

            Comment

            • capablanca
              New Member
              • May 2010
              • 60

              #7
              Hello Rabbit,
              The output that i try to create is:

              1) Print a first string array using 5 for() loops
              2) Print a second string array using 5 for() loops
              3) Print the equals rows between both arrays

              Data:
              Code:
              string[] firstArray = { "3", "5", "8", "14", "17", "18", "20", "21", "23", "25", "26", "27", 
                                                  "29", "32", "34", "36", "37", "39" };
                          string[] secondArray = { "3", "5", "8", "9", "11", "12", "13", "14", "15", "17", "18", "21", 
                                                   "25", "26", "27", "32", "34", "36", "38", "39", "38" };
              for example:
              3 5 8 14 17
              3 5 8 14 18
              3 5 8 14 20

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Why would you need to print a string using 5 for loops? It only takes one loop to print n numbers of elements from an array.

                In your example, I don't understand why the third line is in your output when 20 isn't in both arrays. They don't match according to your third requirement.

                Your requirements don't make any sense.
                Last edited by zmbd; Dec 24 '13, 05:54 PM. Reason: [Z{makes me feel better to hear you say that thought zmaddhatter had me again! (@.@) }]

                Comment

                • capablanca
                  New Member
                  • May 2010
                  • 60

                  #9
                  Hello Rabbit,
                  I need 5 for() loops because i want to output all possibles combinations creating a matrix of size (rows x 5 columns)from each string arrays.
                  In your second question, if you scroll the horizontal scrollbar or expand the text you will see the number 20.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    That explains the for loops.

                    I see the number 20, my confusion wasn't with whether or not the 20 exists. But where it exists. Your third requirement is that it needs to be in both arrays but 20 exists in only one of the arrays.

                    Since your main question is about the speed at which it runs, my guess is that the largest factor in the speed would be that the data grid view rows are being added one at a time. Instead populate an array with the results and then bind the data grid view to that array.

                    Comment

                    • capablanca
                      New Member
                      • May 2010
                      • 60

                      #11
                      Thanks Rabbit, i will try it that.
                      My requirement are:

                      1) Using 5 for() loops to output the first string array.
                      2) Using another 5 for() loops to output the second string array.
                      3) When they are recorded into different int[,] arrays, check if there are equal rows between both array.

                      Comment

                      Working...