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?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdafx.h"
#include <string>
#include <iostream>
#include <cmath>
#include <fstream> 
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<r;i++){
		for(int j=0;j<c;j++){
			cout<<grid[i][j]<<" ";
		}
		cout << endl;
	}
	}
	getline (cin, str);
    getline (cin, str);
	return 0;
}


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

1
2
	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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.