title: Circular array and elimination in C, how to return the last "living" element index?
body: <p>I am trying to write a code that will simulate a <code>circle</code> of a fixed size of people that have one sword. The closest &quot;living person&quot; to the current <code>index</code> will be eliminated and the sword will be passed to the next living person (after the one who got killed) and so on.</p>
  <p>I want it to be written without linked lists.</p>
  <p><strong>Example:</strong>
  A group of 3 people:
  <code>arr[0] = 1, arr[1] = 1, arr[2] = 1</code></p>
  <p><strong>First turn:</strong></p>
  <ul>
  <li><code>arr[0] KILLS arr[1] and the sword gets PASSED to arr[2]</code></li>
  </ul>
  <p><strong>Values of elements after the first turn:</strong></p>
  <p><code>arr[0] = 1, arr[1] = 0, arr[2] = 1</code></p>
  <p><strong>Second turn:</strong></p>
  <ul>
  <li><code>arr[2] KILLS arr[0] and stays the last player</code></li>
  </ul>
  <p><strong>Values of elements after the second turn:</strong></p>
  <p><code>arr[0] = 0, arr[1] = 0, arr[2] = 1</code></p>
  <ul>
  <li><code>arr[2]'s index gets returned by the main function</code>.</li>
  </ul>
  <p>What I thought about was:</p>
  <ul>
  <li>array</li>
  <li>set the values all of elements to <code>1</code></li>
  <li>circulate and check each time <code>if (1 == arr[i])</code></li>
  <li>set a flag to determine whether to kill or just pass the sword to this guy.</li>
  <li>return the current index that indicates that's the index of the last player alive.</li>
  </ul>
  <p>For example, lets say we have 5 people in our group:
  <code>[1] [1] [1] [1] [1]</code></p>
  <p><strong>First round</strong>:</p>
  <p><code>give_sword = 0</code></p>
  <p><code>i = 0</code> does not enter the first <code>if</code> because <code>give_sword</code> is not <code>1</code>.
  It enters the second <code>if</code>, and finds the closest living person using the function <code>findClosestLivingPerson</code> and gets his index and sets his value to <code>0</code> (== kills the closest living person).
  It sets <code>give_sword</code> to <code>1</code>.</p>
  <p>Decreases the <code>players_counter</code> and checks if there is only one player left. If not, continues the loop.</p>
  <p>This is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int findClosestLivingPerson(int arr[], int index, int group_size);
  
  
  int main (int argc, char *argv[])
  {
      
      int group_size = 0, players_counter = 0;
      int i = 0, give_sword = 0;
      int arr[100] = {0};
      
      printf(&quot;Enter group size: \n&quot;);
      scanf(&quot;%d&quot;,&amp;group_size);
      
      for (i = 0; i &lt; group_size; i++)
      {
           arr[i] = 1;
      }
     
      players_counter = group_size;
      
          for (i = 0; i &lt; group_size; (i+1) % group_size)
          {
              if (1 == arr[i])
              {
                  if(1 == give_sword) /* should give sword, not to kill */
                  {
                      give_sword = 0;
                  }
                  else /* should be killed */
                  {
                      arr[findClosestLivingPerson(arr,i, group_size)] = 0;
                      give_sword = 1;
                      --players_counter;
                      if (players_counter == 1)
                      { 
                          break;
                      }
                  }
              }
          }
      printf(&quot;Winner is %d &quot;,i);
      return 0;
  }
  
  int findClosestLivingPerson(int arr[], int index, int group_size)
  {
      for (; index &lt; group_size; (index+1) % group_size)
      {
          if (arr[index] == 1)
          return index;
      }
      return 0;
  }
  </code></pre>
  <p>The compiler says:</p>
  <blockquote>
  <p>In function ‘main’: last_man.c:23:43: warning: statement with no
  effect [-Wunused-value]    23 |         for (i = 0; i &lt; group_size;
  (i+1) % group_size)</p>
  <p>last_man.c: In function ‘findClosestLivingPerson’: last_man.c:49:42:
  warning: statement with no effect [-Wunused-value]    49 |     for (;
  index &lt; group_size; (index+1) % group_size)</p>
  </blockquote>
  <p>The <code>(index+1) % group_size</code> is meant to circulate through this array.</p>