How to compare rows into a 2D array.

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

    How to compare rows into a 2D array.

    How to compare one row with the other ones to see if that row has a match into a 2D array and if there is any match, display it . It is this possible to do it? example:

    Code:
    int*array[rows][cols]*=*{{2,4,5,7,8},{1,3,5,7,8},{3,4,5,7,8},
    		***************************         *{4,6,7,8,9},{2,4,5,7,8}};
    row 1 = row 5

    How can I check this equality?
    Thanks for any explanation.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    This example is not legal C code. What language is it written in?

    Comment

    • capablanca
      New Member
      • May 2010
      • 60

      #3
      Hi donbock.
      The code is this:
      Code:
          array[5][5] = {{2,4,5,7,8},{1,3,5,7,8},{3,4,5,7,8},
                                {4,6,7,8,9},{2,4,5,7,8}};
      row 1 = row 5

      How can I check this equality?
      Thanks for any explanation.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Compare the rows to each other one column at a time. You know the rows are different as soon as you find a column that doesn't match. You don't know the rows are the same until after you have compared the last column.

        By the way, you really want to compare row 0 to row 4.

        Comment

        • capablanca
          New Member
          • May 2010
          • 60

          #5
          Originally posted by donbock
          Compare the rows to each other one column at a time. You know the rows are different as soon as you find a column that doesn't match. You don't know the rows are the same until after you have compared the last column.

          By the way, you really want to compare row 0 to row 4.
          Hi donbock.
          I do not want one-time check if the entire array rows are equal, I know that, using three loops I can make that check, what I really want to know is, if in the array there is a particular row equal to another such as if the row 1 equals row 2, row 3, row 4, row 5 or later if the line 2 is equals to row 3, row 4 or 5 row and so on and if this equality exists display that row. Thanks.

          Comment

          • capablanca
            New Member
            • May 2010
            • 60

            #6
            Hi.
            How to compare one row with the other ones to see if that row has a match into a 2D array and if there is any match, display it . It is this possible to do it? example:
            The code is this:

            Code:
            array[5][5] = {{2,4,5,7,8},{1,3,5,7,8},{3,4,5,7,8},
                                      {4,6,7,8,9},{2,4,5,7,8}};
            row 0 = row 4

            How can I check this equality?

            What I really want to know is, if in the array there is a particular row equal to another such as if the row 1 equals row 2, row 3, row 4, or row 5 and later if the line 2 is equals to row 3, row 4 or 5 row and so on and if this equality exists display that row. Thanks.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Write a function that compares two rows. Have the function return true or false.
              This function will compare two one dimensional arrays.

              Then call the function with the address of two rows.

              Comment

              • capablanca
                New Member
                • May 2010
                • 60

                #8
                Originally posted by weaknessforcats
                Write a function that compares two rows. Have the function return true or false.
                This function will compare two one dimensional arrays.

                Then call the function with the address of two rows.
                Hello weaknessforcats .
                In the following program if you build it and run it you will get:

                2 4 5 7 8
                1 3 5 7 8
                3 4 5 7 8
                4 6 7 8 9
                2 4 5 7 8

                There are differents rows.

                Now my question is:
                How to display in the console the equals rows, in this case there is just one equality(row 0 = row 4) but could be exist more equalities in others cases.
                You told me call the function with the address of two rows but I do not how to do it in this case because they are into a 2D array. Can you help me.
                This is the code:

                Code:
                           #include <iostream>
                #include <iomanip>
                
                using namespace std;
                
                const int rows = 5;
                const int cols = 5;
                
                int main()
                {
                    cout<<endl;
                
                    int array[rows][cols] = {{2,4,5,7,8},
                                            {1,3,5,7,8},
                                            {3,4,5,7,8},
                                            {4,6,7,8,9},
                                            {2,4,5,7,8}};
                
                    for(int i=0; i< rows; i++)
                     {
                       for(int j=0; j< cols; j++)
                        {
                          cout<<setw(7)<<array[i][j];
                        }
                          cout<<endl;
                     }
                    cout<<endl;
                
                    bool equal = true;
                    for(int i=0; i<rows; i++)
                     {
                      for(int j=i; j<rows; j++)
                        {
                          for(int k=0; k<cols && equal;k++)
                            {
                               if(array[i][k] != array[j][k])
                               {
                                   equal = false;
                               }
                            }
                        }
                     }
                    if(equal==0)
                     cout<<setw(33)<<"There are differents rows."<<endl;
                    else //if(equal==1)
                     cout<<setw(33)<<"All the rows are the same."<<endl;
                     cout<<endl;
                
                    return 0;
                }

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  The rows are array[0], array[1], array[2], array[3] and array[4].

                  Call your function:

                  Code:
                  if (MyCompare(array[0],array[4], 5)
                  //etc..
                  where:

                  Code:
                  bool MyCompare(int* first, int* second, int numelements)
                  {
                      //compare the first and second arrays and return true if equal
                      //and false otherwise.
                      //Use a loop that runs from 0 to numelements 
                      //and compare first[i] with second[i]
                      //if this compare fails return false else continue the loop. If you
                      // finish the loop, return true.
                  }
                  Write a nested loop in main top pass the necessary rows to the function.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    BTW have you read this: http://bytes.com/topic/c/insights/77...rrays-revealed ?

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      Your nested loop is good, but you need to detect whether 2 rows are equal, not whether all of them are equal. Put all of the detection, inside the corresponding loops
                      Code:
                      bool equal = false; // any row match yet?
                      for(int i=0; i<rows; i++) 
                      { 
                          for(int j=i; j<rows; j++) 
                          {  
                              bool equalRow = true; // reset the equal variable
                             .. for k compare
                             if after the loop is over equalRow still equals true{
                                 equal = true; // we have a matching row.
                                 then rows i and j are the same. Output it
                              }
                          }

                      Comment

                      • capablanca
                        New Member
                        • May 2010
                        • 60

                        #12
                        Originally posted by jkmyoung
                        Your nested loop is good, but you need to detect whether 2 rows are equal, not whether all of them are equal. Put all of the detection, inside the corresponding loops
                        Code:
                        bool equal = false; // any row match yet?
                        for(int i=0; i<rows; i++) 
                        { 
                            for(int j=i; j<rows; j++) 
                            {  
                                bool equalRow = true; // reset the equal variable
                               .. for k compare
                               if after the loop is over equalRow still equals true{
                                   equal = true; // we have a matching row.
                                   then rows i and j are the same. Output it
                                }
                            }
                        Hi jkmyoung.
                        I have about three months studying on my own C + + and I still do not adapt when someone explain me any questions that I have please could you help me a little more because I think understand what you say but I find it difficult to bring to thought the language of C + +. Thank in advance.

                        Comment

                        • jkmyoung
                          Recognized Expert Top Contributor
                          • Mar 2006
                          • 2057

                          #13
                          Your for loops are sort of like
                          Code:
                          1. For - pick 1st row
                           2. For - pick 2nd row
                            3. Pick index of row
                              Compare element at same index of 1st and 2nd chosen rows.
                          Having two booleans is probably confusing. Just do one first:
                          Code:
                          1. For - pick 1st row
                           2. For - pick 2nd row
                            boolean rowMatch = true
                            3. Pick index of row {
                              Compare element at same index of 1st and 2nd chosen rows. If doesn't match, set rowMatch = false
                            }
                            if rowMatch still true, rows match

                          Comment

                          • capablanca
                            New Member
                            • May 2010
                            • 60

                            #14
                            Originally posted by jkmyoung
                            Your for loops are sort of like
                            Code:
                            1. For - pick 1st row
                             2. For - pick 2nd row
                              3. Pick index of row
                                Compare element at same index of 1st and 2nd chosen rows.
                            Having two booleans is probably confusing. Just do one first:
                            Code:
                            1. For - pick 1st row
                             2. For - pick 2nd row
                              boolean rowMatch = true
                              3. Pick index of row {
                                Compare element at same index of 1st and 2nd chosen rows. If doesn't match, set rowMatch = false
                              }
                              if rowMatch still true, rows match
                            Hello jkmyoung.
                            You really understand the questions I had. I tried to implement your idea in programming language C + + but without any success, if not too much to ask could you create your idea in c + +, and output the row that match . Believe me I have tried to solve this but I can not even with your explanation maybe because I have little time studying C + + .Thanks again.

                            Comment

                            • jkmyoung
                              Recognized Expert Top Contributor
                              • Mar 2006
                              • 2057

                              #15
                              Can you post what you have now? Maybe it's a simple error.

                              Comment

                              Working...