This is a noob question. I have an array called Counter[N][N] and I want to do something like:
While (each element of Counter < 10000) {do something}
While (there exists an element of Counter < 10000) {do something}
Is there an easy way of doing that in C?
This function tests whether the counter array passed in has an element smaller than the specified value:
You use it:
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
<stdbool.h>available and included.Is this what you’re after? You mention ‘While’ so it isn’t clear whether you need to use a
whileloop — if you do, I think this does the job:Or, more colloquially but using
forloops:Note that this code is using C99; you can declare
iandjoutside the loops and it becomes C89 code. Also, if for any reason you neediorj(or, more likely, both) after the loop, you need to declare the variables outside the loop.The second solution with
forloops is more idiomatic C; theforloop 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 thewhileloop solution which has the initialize code on one line (outside the loop), the condition on another, and the reinitialization on yet a third line.