Reputation: 963
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?
Upvotes: 3
Views: 28632
Reputation: 1316
Easy if you can write a function which will return 1 if all the elements are less than 10000 :
int check_array_lt(int row_count, int col_count, int** array, int value)
{
int i,j;
for(i=0;i<row_count;i++)
for(j=0;j<row_count;j++)
if (array[i][j]>=value)
return 0;
return 1;
}
then use it :
while( check_array_lt(N,N,counter,10000) ) {
do something
}
For the second version of the question (no more 'each element < 10000' but 'at least one element < 10000') :
int check_array_lt_atleast(int row_count, int col_count, int** array, int value)
{
int i,j;
for(i=0;i<row_count;i++)
for(j=0;j<row_count;j++)
if (array[i][j]<value)
return 1;
return 0;
}
As stated by Jonathan Leffler, this solution works only if the array is dynamically created; if Counter is declared as an array with #define
d N, than my solution decays in Jonathan's one.
Upvotes: 1
Reputation: 17655
What about this?
int flag=0;
for(i=o;i<n;i++)
{
for(j=o;j<n;j++)
{
if(counter[i][j]<10000)
//statements;
else
{
flag=1;
break;
}
}
if(flag==1)
break;
}
Upvotes: 0
Reputation: 753525
This function tests whether the counter array passed in has an element smaller than the specified value:
bool has_element_less_than(int value, int counter[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (counter[i][j] < value)
return true;
}
}
return false;
}
You use it:
if (has_element_less_than(10000, counter))
do_something();
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 while
loop — if you do, I think this does the job:
int i = 0;
while (i < N)
{
int j = 0;
while (j < N)
{
if (counter[i][j] < 10000)
{
counter[i][j] = do_something(i, j, counter[i][j]);
}
j++;
}
i++;
}
Or, more colloquially but using for
loops:
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (counter[i][j] < 10000)
{
counter[i][j] = do_something(i, j, counter[i][j]);
}
}
}
Note that this code is using C99; you can declare i
and j
outside the loops and it becomes C89 code. Also, if for any reason you need i
or j
(or, more likely, both) after the loop, you need to declare the variables outside the loop.
The second solution with for
loops is more idiomatic C; the for
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 while
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.
Upvotes: 2
Reputation: 9418
this can be nicely done with pointers
while(true)
{
int* pEnd = &Counter[0][0] + N*N;
int* pCurrent = &Counter[0][0];
bool AnyLess = false;
while(pCurrent < pEnd && !AnyLess) { AnyLess |= *pCurrent++ < 10000; }
if(!AnyLess)
break;
}
Upvotes: 1
Reputation: 9418
although you did not ask for c#, here is the c# solution because it would be so easy:
var Counter = new int[N,N];
while(Counter.Cast<int>.Any(i => i < 10000)) { dosomething(); }
Upvotes: 0
Reputation: 28741
Solution for EDITED question
for(i=0;i<N;i++)
for(j=0;j<N;j++){
if(counter[i][j]<10000)
{
//Do something
}else
goto OUT;
}
OUT:
printf("out of loops");
In my arrogant opinion
goto
statement is the most elegant construct in programming language . If anybody wants to become billionaire like Bill Gates , they must use lots and lots of goto's while developing their path breaking software .
Upvotes: 0
Reputation: 147
You could do
for(int x = 0; x < N; x++) {
for(int y = 0; y < N; y++) {
if (Counter[x][y] < 10000){
//Do something with Counter[x][y]
}
}
}
Upvotes: 2
Reputation: 28741
Use nested for loop
for(i=0;i<N;i++)
for(j=0;j<N;j++){
if(counter[i][j]<10000)
{
//Do something
}
}
Upvotes: 0