Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This function tests whether the counter array passed in has an element smaller than the specified value:</p> <pre><code>bool has_element_less_than(int value, int counter[N][N]) { for (int i = 0; i &lt; N; i++) { for (int j = 0; j &lt; N; j++) { if (counter[i][j] &lt; value) return true; } } return false; } </code></pre> <p>You use it:</p> <pre><code>if (has_element_less_than(10000, counter)) do_something(); </code></pre> <p>You could deal with variable dimension arrays in C99 by passing N as a parameter to the function. It assumes you have the C99 header <code>&lt;stdbool.h&gt;</code> available and included.</p> <hr> <p>Is this what you're after? You mention 'While' so it isn't clear whether you need to use a <code>while</code> loop — if you do, I think this does the job:</p> <pre><code>int i = 0; while (i &lt; N) { int j = 0; while (j &lt; N) { if (counter[i][j] &lt; 10000) { counter[i][j] = do_something(i, j, counter[i][j]); } j++; } i++; } </code></pre> <p>Or, more colloquially but using <code>for</code> loops:</p> <pre><code>for (int i = 0; i &lt; N; i++) { for (int j = 0; j &lt; N; j++) { if (counter[i][j] &lt; 10000) { counter[i][j] = do_something(i, j, counter[i][j]); } } } </code></pre> <p>Note that this code is using C99; you can declare <code>i</code> and <code>j</code> outside the loops and it becomes C89 code. Also, if for any reason you need <code>i</code> or <code>j</code> (or, more likely, both) after the loop, you need to declare the variables outside the loop.</p> <p>The second solution with <code>for</code> loops is more idiomatic C; the <code>for</code> loop is very good for this job and is what you should plan to use, not least because it packages all the loop controls on a single line, unlike the <code>while</code> loop solution which has the initialize code on one line (outside the loop), the condition on another, and the reinitialization on yet a third line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload