Loop

Dear Friends
I want to stop the following code when all entities in the error[i][j] are simultaneously less than one. And if even one entity be greater than one I want to continue running the code.
Can someone suggest me any solution to the problem?

1
2
3
4
5
6
7
  	for (i=0;i<=3;i++)
{		
for (j=0;j<3;j++)
	{
	error[i][j]= fabs(T[i][j]-TOLD[i][j]);
	}
}
Did you mean like this?
1
2
3
4
5
6
7
8
9
10
11
12
for (i=0;i<=3;i++)
{
   bool is_one = true;	
   for (j=0;j<3;j++)
   {
      error[i][j]= fabs(T[i][j]-TOLD[i][j]);
      if(error[i][j] >= 1)
         is_one = false;
   }
   if(is_one == true)
      break;
}
Last edited on
@ Grime
Thanks for your reply, but your code is breaking even for single value of error[i][j] is greater than 1.
I want to make all nine values of error[i][j] less than one simultaneously.
Regards
uSMAN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while 
{
   bool break_condition = true;	
   for (i=0;i<=3;i++)
   {
      for (j=0;j<3;j++)
      {
         error[i][j]= fabs(T[i][j]-TOLD[i][j]);
         if(error[i][j] >= 1) 
         {
            is_one = false;
            break;
         }
      }
      if(!is_one)
         break;
   }
   if(break_condition == true)
      break; // if all values are lesser than 1
}
> I want to make all nine values of error[i][j] less than one simultaneously.
Your outer loop loops 4 times, and the inner one three times.

How about some bit magic?
1
2
3
4
5
6
7
8
9
10
    unsigned int keepgoing;
    do {
        keepgoing = 0;
        for (int i=0;i<=3;i++) {
            for (int j=0;j<3;j++) {
                error[i][j] = fabs(T[i][j]-TOLD[i][j]);
                keepgoing |= error[i][j] > 1 ? 1<<(i*3+j) : 0;
            }
        }
    } while ( keepgoing );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cmath>   // for abs()
using namespace std;

const int NI = 3, NJ = 3;


bool isConverged( double T[NI][NJ], double TOLD[NI][NJ] )
{
   const double TOLERANCE = 1.0;    // Too high for convergence tolerance

   for ( int i = 0; i < NI; i++ )
   {
      for ( int j = 0; j < NJ; j++ )
      {
         if ( abs( T[i][j] - TOLD[i][j] ) > TOLERANCE ) return false;
      }
   }

   return true;                   // No large changes, so judge "converged"
}



However, you would be better having no array TOLD[][] at all. Just calculate the maximum change occurring at the same time as you do the update step on T[][].

If, at the end of the sweep, this maximum change exceeds your threshold (1.0 is actually rather large) then you have to keep sweeping through your array.
Last edited on
Friends thanks for your help. I used the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                          for (i=0;i<=grid;i++)
			{		
				for (j=0;j<grid;j++)
				{
					error[i][j]= fabs(T[i][j]-TOLD[i][j]);
					if(error[i][j]>=1)
					{
	                                continue;
					}
					else
					{
						break;
					}
				}
			}
Last edited on
What do you think that is doing?
> I used the following code.
Are you deliberately posting code with the worst indentation possible?

I used the following code.

But it doesn't work. It breaks out of the inner loop, but the outer loop runs again. So this code stops updating each row if it detects a greater-than-one item in the row, but it then it moves on to the update the next row.
It worked well in my full code. I think the code will only break when for all values of outer loop the if condition is not fulfilled.
Topic archived. No new replies allowed.