How can I simplify this code?

Hi,Im writing a code where I have a 10 by 10 array. Each node holding information such as X and Y location and the ID of the node.
Example
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16...............
21..........14 25 26 27...........
31 ..................................
.....................................
....................................100
And when I the user input the location, say 15, my code would show this:
4 5 6
14 15 16
24 25 26

the X and Y information such like X= 5 and Y = 1

This is the code I came up with but I'm struggling to simplify this.


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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 
 #include<iostream>

using namespace std;
#define HEIGHT 10
#define WIDTH 10


struct Grid {
	int x; //to show x line
	int y; //to show y line
	int id; //to show the grid id
};


int menu() {
	int choice;
	cout << endl << endl;
	cout << "::MENU::" << endl <<
		"[1] Check grid by ID" << endl <<
		"[2] Display grid" << endl <<
		"[3] Exit" << endl <<
		"Enter choice: ";
	cin >> choice;

	return choice;
}

//Grid display by ID and its neighbouring ID
void checkbyid(Grid grid[HEIGHT][WIDTH]) {
	cout << "What is the ID?";
	int id;
	int a,b;
	cin >> id;
	for (int i = 0; i<HEIGHT; i++) {
		for (int x = 0; x<WIDTH; x++) {
			if (grid[i][x].id == id) {

				for (a = i-1; a<i + 2; a++) {
					if (a < 0|| a>9) continue;
					for (b = x - 1; b < x + 2; b++) {
						if (b < 0 || b>9) continue;
						else {
							cout << grid[a][b].id << "\t";
						}
					}
					cout << endl;
					}
				}
			}
	}
	cout << "Which surrounding ID's X,Y axis would you like to view? (0 to exit)";
	cin >> id;
	for (int i = 0; i < HEIGHT; i++) {
		for (int x = 0; x < WIDTH; x++) {
			if (grid[i][x].id == id) {
				cout << "X: " << grid[i][x].x << "\t";
				cout << "Y: " << grid[i][x].y << "\t";
			}
			else if (id == 0) break;
		}
	}
}



//display the whole grid
void displayGrid(Grid grid[HEIGHT][WIDTH]) {
	for (int i = 0; i<HEIGHT; i++) {
		for (int x = 0; x<WIDTH; x++) {
			//cout << "X:" << grid[i][x].x;
			//cout << "Y:" << grid[i][x].y;
			cout << grid[i][x].id<<"\t";
		}
		cout << endl;
	}

}
//main function

int main() {
	int choice;
	int id = 1;
	Grid grid[HEIGHT][WIDTH];
	for (int i = 0; i<HEIGHT; i++) {
		for (int x = 0; x<WIDTH; x++) {
			grid[i][x].x = i;
			grid[i][x].y = x;
			grid[i][x].id = id;
			id++;
		}
	}
	cout << endl;
	do {
		choice = menu();
		switch (choice) {
		case 1:
			cout << "Option [1] Check Grid by ID" << endl;
			checkbyid(grid);
			break;

		case 2:
			cout << "Option [2]Display Grid" << endl;
			displayGrid(grid);
			break;
		case 3:
			break;
		}

	} while (choice != 3);

	cout << "Exit program. Thank you "<< endl;


	return 0;
}


Im sorry if its messy.

Thank you
x/y location and ID seem redundant. am i missing something or are the index [r][c] sufficient to ID/locate a node??

the only thing complex is check by id.
Topic archived. No new replies allowed.