### help with a goto loop

I'm trying to write a program that creates a grid with set values filling each of the 4 outer edges, then fills in the other squares with the average value of their 4 adjacent squares, and keeps doing that until the grid becomes more or less constant.

The program creates grids 1 and 2 with the constant squares filled in and everything else set to 0, averages all the variable squares one at a time in grid 1, then checks grid 1 against grid 2 one square at a time. If any point on grid 1 is more than .001 away from the same point on grid 2, it sets "f" to 1, and then copies grid 1 to grid 2. After the copying, it prints f, checks if f=1, and if so, it goes back to the averaging step, where f is reset to 0. What should happen is, once grid 1 and grid 2 are almost exactly the same, f will stop being set to 1, so the if statement will register false, the goto statement won't trigger, and it'll move onto the next step of printing the grid out. But it's triggering the goto statement whether or not f is 1.

Here's the code, can anyone find the problem?:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````#include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string str; int f=0; int c=10; int r=10; int y=0; int x=0; int x1=0; int y1=0; int x2=0; int y2=0; int t1,t2,t3,t4; cout << "Temp top: "; cin >> t1; cout << "Temp right: "; cin >> t2; cout << "Temp bottom: "; cin >> t3; cout << "Temp left: "; cin >> t4; double grid[10][10]; for(y=0; y<10;y++){ for(x=0; x<10;x++){ grid[y][x]=0; } } for(x=0,y=0; x<10;x++){ grid[y][x]=t1;} for(x=10,y=0; y<10;y++){ grid[y][x]=t2;} for(x=9,y=1; y<9;y++){ grid[y][x]=t2;} for(x=0,y=9; x<10;x++){ grid[y][x]=t3;} for(x=0,y=1; y<9;y++){ grid[y][x]=t4;} double grid2[10][10]; for(y=0; y<10;y++){ for(x=0; x<10;x++){ grid2[y][x]=grid[y][x]; } } averaging: f=0; for(y=1; y<9;y++){ for(x=1; x<9;x++){ grid[y][x]=((grid[y-1][x]+grid[y+1][x]+grid[y][x-1]+grid[y][x+1])/4); } } for(y=1; y<9;y++){ for(x=1; x<9;x++){ if (grid[y][x]-grid2[y][x]>.001){f=1;}; if (grid2[y][x]-grid[y][x]>.001){f=1;}; } } for(y=0; y<10;y++){ for(x=0; x<10;x++){ grid2[y][x]=grid[y][x]; } } cout<< f; if (f = 1) { goto averaging; } else if (f = 0) { for(int i=0; i

Thanks.
Anyone?
First why are you using goto? This loop construct should only be used very rarely, you should re-factor your code to use one of the other loop constructs.

Next remember that operator= is assignment.

What's wrong with goto? It's the easiest loop for this situation. Hell, it's the easiest loop for just about every situation.

And I don't know what "operator= is assignment" means. Should I be using "if (1 = f)"?
Last edited on
Take into account that this loop

 ``12`` `````` for(x=10,y=0; y<10;y++){ grid[y][x]=t2;}``````

is invalid because there is no such element as for example grid[0][10]
because the acceptable range of indexes of the array is [0,9]
Last edited on
 What's wrong with goto?

The goto loop is considered a very bad practice because it leads to non-structured programming. You should learn to properly use functions and the other loop constructs. In this case I suggest a simple do/while{} or while loop.

 And I don't know what "operator= is assignment" means. Should I be using "if (1 = f)"?

Do you know the the difference between assignment and comparison?

 In this case I suggest a simple do/while{} or while loop.

That would be something like
 ``123456789101112131415161718192021222324`` ``````do { f=0; for(y=1; y<9;y++){ for(x=1; x<9;x++){ grid[y][x]=((grid[y-1][x]+grid[y+1][x]+grid[y][x-1]+grid[y][x+1])/4); } } for(y=1; y<9;y++){ for(x=1; x<9;x++){ if (grid[y][x]-grid2[y][x]>.001){f=1;}; if (grid2[y][x]-grid[y][x]>.001){f=1;}; } } for(y=0; y<10;y++){ for(x=0; x<10;x++){ grid2[y][x]=grid[y][x]; } } cout<< f; } while (f=1);``````

Yes?

 Do you know the the difference between assignment and comparison?

No.
Last edited on
In C/C++ there is a different operator for assignment, operator= and comparison, operator==. When you want to compare a value use the comparison operator. When you want to assign a value to an object use the assignment operator.

And other than the use of the incorrect operator your loop looks valid.

Last edited on
Oh, thanks.
Topic archived. No new replies allowed.