Cyclic Array || Wrap-around Array

I've been trying to solve different problems with arrays(through pointers).
Yesterday, I came across a problem which is giving me a lot of trouble.
The purpose of my program is to find a 2D-array within a another 2D-array.

I can't figure out what to do when an element is found at the edge of the array. For example if 15 is found then I need to check whether the next element is 3 or not. How am I supposed to do it?

~Source~
+-----+-----+-----+----+
| 1 | 2 | 3 | 4 |
+----------------------+
| 5 | 6 | 7 | 8 |
+----------------------+
| 9 | 10 | 11 | 12 |
+----------------------+
| 13 | 14 | 15 | 16 |
+-----+-----+-----+----+

~Target~
+-----+-----+
| 8 | 5 |
+-----------+
| 12 | 16 |
+-----+-----+

OR

+-----+-----+
| 4 | 1 |
+-----------+
| 16 | 13 |
+-----+-----+

OR

+-----+-----+
| 14 | 15 |
+-----------+
| 2 | 3 |
+-----+-----+


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
#include<iostream>
using namespace std;


void col_check(int ** source, int s_row, int s_col, int ** target, int t_row, int t_col, int f_row, int f_col)
{
	if(f_col!=s_col)
	{
			for(int b=0; b<t_col; b++)
			{
				if(f_row==s_row-1)
				{

				}
			}
	}
}

void search_index(int ** source, int s_row, int s_col, int ** target, int t_row, int t_col)
{
	for(int i=0; i<s_row; i++)
	{
		for(int j=0; j<s_col; j++)
		{
			if(source[i][j]==target[0][0])
			{
				/*found =*/ col_check(source, s_row, s_col, target, t_row, t_col, i, j);
			}
		}
}

int main()
{
	int ** source, ** target;

	source = new int * [4];
	for(int i=0; i<4; i++)
	{
		source[i] = new int [4];
	}

	for(int i=0, m=1; i<4; i++)
		for(int j=0; j<4; j++, m++)
			source[i][j] = m;

	target = new int * [2];
	for(int i=0; i<2; i++)
	{
		target[i] = new int [2];
	}

	cout << "Enter four array values to be searched: ";
	for(int i=0; i<2; i++)
		for(int j=0; j<2; j++)
			cin >> target[i][j];

	cout << "\n~Source Array~" << endl;
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
			cout << source[i][j] << "\t";
		cout << endl;
	}

	cout << "\n~Target Array~" << endl;
	for(int i=0; i<2; i++)
	{
		for(int j=0; j<2; j++)
			cout << target[i][j] << "\t";
		cout << endl;
	}

	search_index(source, 4, 4, target, 2, 2);


	delete source;
	delete target;
	system("pause");
	return 0;
}
Last edited on
closed account (48T7M4Gy)
Use modulo arithmetic.

e.g. if there are 4 rows numbered 1,2,3,4 then the row "next" to row 4 is [row(4+1) mod 4] which is row 1 etc

Same principle for columns and for all the other directions.
@kemort - Thanks very much. Such a simple solution to such a complicated 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
#include<iostream>
using namespace std;

void search_index(int ** source, int s_row, int s_col, int ** target, int t_row, int t_col)
{
	for(int i=0; i<s_row; i++)
	{
		for(int j=0; j<s_col; j++)
		{
			if(source[i][j]==target[0][0])
			{
				for(int a=0; a<t_row; a++)
				{
					for(int b=0; b<t_col; b++)
						if(source[(i+a)%s_row][(j+b)%s_col]==target[a][b])
						{
							cout << source[(i+a)%s_row][(j+b)%s_col] <<  " - Matched\n\n" << endl;
						}
				}
			}
		}
	}
}

int main()
{
	int ** source, ** target;

	source = new int * [4];
	for(int i=0; i<4; i++)
	{
		source[i] = new int [4];
	}

	for(int i=0, m=1; i<4; i++)
		for(int j=0; j<4; j++, m++)
			source[i][j] = m;

	target = new int * [2];
	for(int i=0; i<2; i++)
	{
		target[i] = new int [2];
	}

	cout << "Enter four array values to be searched: ";
	for(int i=0; i<2; i++)
		for(int j=0; j<2; j++)
			cin >> target[i][j];

	cout << "\n~Source Array~" << endl;
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
			cout << source[i][j] << "\t";
		cout << endl;
	}

	cout << "\n~Target Array~" << endl;
	for(int i=0; i<2; i++)
	{
		for(int j=0; j<2; j++)
			cout << target[i][j] << "\t";
		cout << endl;
	}

	search_index(source, 4, 4, target, 2, 2);


	delete source;
	delete target;
	system("pause");
	return 0;
}


That's all folks!


Enter four array values to be searched: 16 13 4 1

~Source Array~
1   2    3   4
5   6    7   8
9   10  11 12
13 14  15 16

~Target Array~
16 13
4   1
16 - Matched

13 - Matched

4 - Matched

1 - Matched

Press any key to continue . . .
Topic archived. No new replies allowed.