How to compare arrays to see if they are equal

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

    #1

    How to compare arrays to see if they are equal

    How can I compare arrays to see if they are equal, they have the same amount of elements for example:

    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}};
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Compare element by element to see if all the elements are equal.

    Comment

    • sheff
      New Member
      • May 2010
      • 21

      #3
      I am nt sure if this will help or not as I am new to C, anyway, the idea of calculating the size of the bytes for the whole array with respect to one of its elements can be used to know the number of elements the array has, as shown below.

      Code:
      { 
        int i;
      int w;
        int b []= { 18.8,47,5,67,45,78,23};
        
        sizeof(b [0]);
      
      w=sizeof(b)/sizeof(b [1]);
       
       
       printf("\n The total number is %d",w);
          getch();
          return 0;
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        w=sizeof(b)/sizeof(b [1]);
        Be carful doing this. It only works for arrays on the local stack.
        It nevers works for arrays allocated on the heap.

        If you pass this array to a function, all the function receives is the address of the array. Then if you write code like this you get the sizeof the pointer divided by the sizeof the element.

        There is no surefire way to calculate the number of elements in a array. I suggest you keep a const variable handy that contains the number of elements.

        Comment

        • capablanca
          New Member
          • May 2010
          • 60

          #5
          Originally posted by capablanca
          Hi.
          How can I compare arrays to see if they are equal, they have the same amount of elements for example:

          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}};
          HI Banfa.
          What I really want to do is compare each row to see if there is equals rows into the 2D arrays, I know I have to compare element by element to see if all the elements are equal , to do this I think I have to use two for loops, but I am not sure what conditions to use inside the loops. Can you help me with that. Thanks.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You will need to use 3 loops.

            The first 2 loops define the 2 rows you are comparing, The outer one needs to iterate through all the rows, the inner one needs to iterate through all the rows following the current outer one.

            The 3rd loop iterates through the elements in the row to compare them.

            Comment

            • capablanca
              New Member
              • May 2010
              • 60

              #7
              Originally posted by Banfa
              You will need to use 3 loops.

              The first 2 loops define the 2 rows you are comparing, The outer one needs to iterate through all the rows, the inner one needs to iterate through all the rows following the current outer one.

              The 3rd loop iterates through the elements in the row to compare them.
              Hi Banfa.
              I am sure what you told me it is the right answer to my question, can you check my code to see what is wrong with the three loops because I get the error forbids assignment of arrays in line 32, besides I am not sure if I did your idea right. THANKS. 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(5)<<array[i][j];
                      }
                        cout<<endl;
                   }
                  cout<<endl;
                  
                  int k =0;
                  for(int i=0; i<rows; i++)
                   {
                    for(int j=0; j<rows; j++)
                      {
                        if(array[i] == array[j])
                         {
                           array[k]=array[i];
                           k++;
                         }
                      }
                   }
                  cout<<setw(5)<<array[k]<<endl<<endl;
              
                  return 0;
              }

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                OK Line 28 you should initialise j to i (j = i) otherwise you are needlessly checking rows that have already been checked.

                I said you need 3 loops, you only have 2. Lines 30 -34 should encased in another loop for columns, remember i and j are both row indexes. To check if rows are equal the usual method is to assume they are and set a flag and then clear the flag if you find an element that differs.

                Code:
                bool equal = true;
                for(int k=0; k<cols && equal;k++)
                {
                    if(array[i][k] != array[j][k])
                    {
                        // The elements are not equal
                        equal = false;
                    }
                }
                
                // equal is true if the rows are the same

                Comment

                • capablanca
                  New Member
                  • May 2010
                  • 60

                  #9
                  Originally posted by Banfa
                  OK Line 28 you should initialise j to i (j = i) otherwise you are needlessly checking rows that have already been checked.

                  I said you need 3 loops, you only have 2. Lines 30 -34 should encased in another loop for columns, remember i and j are both row indexes. To check if rows are equal the usual method is to assume they are and set a flag and then clear the flag if you find an element that differs.

                  Code:
                  bool equal = true;
                  for(int k=0; k<cols && equal;k++)
                  {
                      if(array[i][k] != array[j][k])
                      {
                          // The elements are not equal
                          equal = false;
                      }
                  }
                  
                  // equal is true if the rows are the same
                  Hi Banfa
                  Thank you very much for your help.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Are you suggesting that you need to verify that two different arrays have the same number of rows and columns? I'm not aware of any way to do that at run-time.

                    Comment

                    Working...