-1

So I'm trying to compare the values of these 2 arrays, and I created a test comparing each element of each array to each other. However I only want the printf statement to print once only if all the elements of each array are equal to each other.

This code prints the statement for each element that is equal, but I only need it to print once if all the elements are the same. What should I do?

int MatrixEqualsActual[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int MatrixEquals[3][3] = {{3, 5, 9}, {1, 2, 6}, {9, 0, 1}};

int i, j;
for (i = 0; i < 3; i++) { 
    for (j = 0; j < 3; j++) { 
        if(MatrixEqualsActual[i][j] == MatrixEquals[i][j]) 
        {    
            printf("PASSED (2/2): MatrixEquals()\n");
        }
    }
}

3 Answers3

5

Just use a flag, and print only if it is set! Like:

...
bool flag = true;
for (i = 0; i < 3; i++)
{ 
    for (j = 0; j < 3; j++)
    { 
        if(MatrixEqualsActual[i][j] != MatrixEquals[i][j]) 
        {   
            flag = false;
            break; // we break since we already know that at least one element doesn't match.
        }
   }
}
if(flag)
{
    printf("PASSED (2/2): MatrixEquals()\n");
}

Just remember to #include <stdbool.h> or you will not be able to use boolean type in C. If you don't want to include that, an int variable will do just good!

CinCout
  • 9,486
  • 12
  • 49
  • 67
3

You can keep a flag in the loop that says whether or not the matrices are equal:

int areEqual = 1;
for(...){
    for(...){
        if (MatrixEqualsActual[i][j] != MatrixEquals[i][j]){
            areEqual = 0;
            break; //stop checking by breaking out of the loop
        }
    }
}
if (areEqual) printf("They're the same matrix\n");
Arc676
  • 4,445
  • 3
  • 28
  • 44
1

There are three possible ways to implement this without using break - In C/C++ break can only terminate the innermost loop, which is not really what you are after.

Version 1 using a simple return:

int i, j;
for (i = 0; i < 3; i++) { 
   for (j = 0; j < 3; j++) { 
      if (MatrixEqualsActual[i][j] != MatrixEquals[i][j]) 
      {    
          return;
      }
   } 
}
printf("PASSED (2/2): MatrixEquals()\n");    

Version 2 using goto:

int i, j;
for (i = 0; i < 3; i++) { 
    for (j = 0; j < 3; j++) { 
       if (MatrixEqualsActual[i][j] != MatrixEquals[i][j]) 
       {    
           goto mark;
       }
    } 
}
printf("PASSED (2/2): MatrixEquals()\n");
mark: {} // Matrices do not equal

Version 3 using a flag:

int i, j;
int flag = 1;
for (i = 0; (i < 3) && flag; i++) { 
    for (j = 0; (j < 3) && flag; j++) { 
        if (MatrixEqualsActual[i][j] != MatrixEquals[i][j]) 
        {    
            flag = 0;
        }
    } 
}
if (flag) {
    printf("PASSED (2/2): MatrixEquals()\n");
}

In addition you may want to replace your hardcoded 3 in the looping conditions with a computed length like so:

size_t length_outer = sizeof(MatrixEqualsActual) / sizeof(MatrixEqualsActual[0]);
size_t length_inner = sizeof(MatrixEqualsActual[0]) / sizeof(MatrixEqualsActual[0][0]);
Community
  • 1
  • 1
morido
  • 1,027
  • 7
  • 24
  • Version 1 doesn't provide any helpful information because no info is returned and nothing is printed when the matrices are not equal. Version 2 uses `goto`, which isn't recommended. It's not _that_ bad here, but it's not a good habit to get into. It reduces readability. – Arc676 Jan 27 '16 at 01:09
  • 2
    @Arc676 You can trivially amend Version 1 to return something if desired. Whether or not `goto` is bad practise is certainly a field of debate - but bluntly stating it "is not recommended" is too bold IMO. Printing a message when the matrices are not equal was no requirement by the OP and [your answer](http://stackoverflow.com/a/35010370/5717099) doesn't provide this functionality, either. – morido Jan 27 '16 at 04:10
  • I didn't say printing it was a requirement. I just thought it would be useful to mention that a `return` with nothing else after it, so no information comes out of the function. I didn't mean any offense. – Arc676 Jan 27 '16 at 04:22