title: Scrambled array in c
body: <p>I’m doing a scrambled array where I have to compare two arrays to see if their contents match. I have looked at other scrambled array solutions but for my assignment I cannot change the arrays in any way (no sorting). </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int scrambled(unsigned int a[], unsigned int b[], unsigned int len){
  
      int firstCheck = 0;
      int secondCheck = 0;
      int i = 0;
      int j = 0;
  
      for(i=0; i &lt; len; i++){
          firstCheck = 0;
  
          for(j=0; j&lt; len; j++){
              if(a[i] == b[j]){
                  firstCheck = 1;
  
              }
  
          }
  
          if(firstCheck != 1){
              firstCheck = 0;
              break;
          }
      }
  
  
      for(j=0; j &lt; len; j++){
          secondCheck = 0;
  
          for(i=0; i&lt; len; i++){
              if(b[j] == a[i]){
                  secondCheck = 1;
              }
  
          }
  
          if(secondCheck != 1){
              secondCheck = 0;
              break;
          }
      }
  
      if(len == 0){
          return 1;
      }else if (firstCheck == 0 || secondCheck == 0){
          return 0;
      }else{
          return 1;
      }
  
  }
  </code></pre>
  
  <p>I was given some array examples to test if the code works and all of them work. If the arrays are empty, it should return 1;. My code passed through all the tests but the grading program doesn’t accept my code. I am wondering if I am missing a crucial check?</p>
  
  <p>Examples of arrays for which scrambled should return 1:</p>
  
  <p><code>a = {10,15,20}, b = {10,15,20}</code></p>
  
  <p><code>a = {99}, b = {99}</code></p>
  
  <p><code>a = {1,2,3,4,5}, b = {5,3,4,2,1}</code></p>
  
  <p><code>a = {}, b = {} (i.e. len = 0)</code></p>
  
  <p><code>a = {2,1,3,4,5}, b = {1,2,4,3,5}</code></p>
  
  <p>Examples of arrays for which scrambled should return 0:</p>
  
  <p><code>a = {1,1}, b = {1,2}</code></p>
  
  <p><code>a = {10,15,20}, b = {10,15,21}</code></p>
  
  <p><code>a = {1,2,3,4,5}, b = {5,3,4,2,2}</code></p>
  
  <p>I do have a main code for testing, I’ll post it just in case.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  int scrambled (unsigned int a[], unsigned int b[], unsigned int len);
  int main(){
      unsigned int a[5] = {1,2,3,4,5};
      unsigned int b[5] = {5,3,4,2,2};
      bool result;
  
      result = (scrambled(a,b,5));
  
      if(result == 1){
          printf("b is a scrambled version of a\n");
      }else{
          printf("b is NOT a scrambled version of a\n");
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Thanks for all the replies! I'll try cleaning up my code and trying the methods mentioned (auxiliary arrays/helper function/return statements). I'll make sure to tag only one C next time as I learned they are very different. I'm glad to hear it isn't a logic problem, so perhaps I read the instructions wrong. Posted them below if anyone wants to look. (for #4 it says iff arrays, I assumed it was a typo, is it?)</p>
  
  <p>1) Create a new file called scrambled.c, containing a single function that matches this declaration:</p>
  
  <p>2) int scrambled( unsigned int a[], unsigned int b[], unsigned int len );</p>
  
  <p>3) Arrays a and b are both of length len, and contain values in the range [0 99] inclusive, only.</p>
  
  <p>4) The function scrambled() should return 1 iff arrays a and b contain the same values in any order, or 0 otherwise.</p>
  
  <p>5) len can have any unsigned int value, including 0.</p>
  
  <p>6) If len is 0 then scrambled() should return 1 (since the arrays have the same - empty - contents).</p>
  
  <p>7) You must not change the contents of the arrays.</p>
  
  <p>8) Use an algorithm that has run time linear in the array length n. Note that this means you can not sort the arrays since that can not be done in linear time..</p>
  
  <p>Will update if I manage to figure this out!</p>