473,409 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

How to seach a 2d array for another smaller 2d array

Hey, I'm learning c++ and a program that I am suppose to make is
suppost to read in two .txt files (map.txt, and template.txt). I am
then suppost to put each character into a 2d array (one for each .txt
file). Then try and find if the combination in the template 2d array
is somewhere within the map 2d array. Then print out the what row and
column that it was found at.

I have gotten the two files into seperate 2d arrays:

char ** map = new char*[numLines];

for (int i = 0; i < numLines; i++)
map[i] = new char [length+1];

in.clear();
in.seekg(0);

for (int i = 0; (i < numLines) && (!in.eof()); i++)
{
in.getline(buffer, 100);
strcpy(map[i], buffer);
}

and

char ** temp = new char*[numLines2];

for (int i = 0; i < numLines2; i++)
temp[i] = new char [length2+1];

inTemp.clear();
inTemp.seekg(0);

for (int i = 0; (i < numLines2) && (!inTemp.eof()); i++)
{
inTemp.getline(buffer, 100);
strcpy(temp[i], buffer);
}

But now I am suppost to see if the temp array is inside the map array,
and I'm abit lost. I got the program to work for the default files:

map:
j2DikxdiLLI3j
aAxzZxxixxkw9
okxjzxxxnchnq
jmxmbkklmnsff

temp:
xx
xx

to output the correct information:"Located at row 1, column 5"

//Searching for the temp in the map file

bool found = 0;
int loccol = 0;
int locrow = 0;

for (int i = 0; (i < numLines - 1) && !found; i++) // i runs through
rows
{
for (int j = 0; (j < length - 1) && !found; j++) // j runs through
cols
{
if(map[i][j] == temp[0][0] && map[i+1][j] == temp[1][0] &&
map[i][j+1] == temp[0][1] && map[i+1][j+1] == temp[1][1])
{
found = 1;
loccol = j; // j (columns) are the x coordinate
locrow = i; // i (rows) are the y coordinate
}
}
}

if (found)
cout << "Located at row " << locrow << ", column " << loccol << endl;
else
cout << "Not found" << endl;
But now I need to get it to search if the arrays chage size, and I
can't figure out how to do it.

Any help would be appreciated.

Thanks

Mar 8 '06 #1
1 1567
Kevin wrote:
[..]
for (int i = 0; (i < numLines - 1) && !found; i++) // i runs through
rows
{
for (int j = 0; (j < length - 1) && !found; j++) // j runs through
cols
{
if(map[i][j] == temp[0][0] && map[i+1][j] == temp[1][0] &&
map[i][j+1] == temp[0][1] && map[i+1][j+1] == temp[1][1])
{
found = 1;
loccol = j; // j (columns) are the x coordinate
locrow = i; // i (rows) are the y coordinate
}
}
}

if (found)
cout << "Located at row " << locrow << ", column " << loccol << endl;
else
cout << "Not found" << endl;
But now I need to get it to search if the arrays chage size, and I
can't figure out how to do it.


The 'if' statement inside the loop needs to be converted into two more
nested loops -- going through the elements of the smaller "template".

for (int i = ...
for (int j = ...
// optimization - only start looping if the top left corner
// of the 'template' is not the same as the current
if (map[i][j] == temp[0][0]) {
for (int n = ... // all rows of the template
for (int m = ... // all columns of the template
if (not ... // not equal
goto next_main_element;
} // end-for(m)
} // end-for(n)
found = 1;
} // end-if
next_main_element:
} // end-for (j)
} // end-for (i)

Also, there is no need to iterate through all elements of the main matrix,
only through the top left part leaving the stripe as wide as the template
width/height minus 1 on the right and on the bottom, because the template
would not fit there for comparison.

V
--
Please remove capital As from my address when replying by mail
Mar 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tom | last post by:
Hi I am looking for an optimal data-structure in order to replace a map<int,float>. I use it as some kind of sparse array representation. The facts: - the population in the data-structures...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
2
by: wing ver ka gundam | last post by:
THanks~ this is the example and my quote are below Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will...
15
by: Logan | last post by:
/* Return B from ABC I get segementation error when I execute this. Please help. */ char* getSubstring(char* larger, int a, int b) { char* smaller; int i; for(i=0; i<b; i++) {...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
14
by: =?Utf-8?B?RWR3YXJk?= | last post by:
Hi everybody, To me the following code shouldn't work but it does ! Imports system.String Dim x As String="This,is,a,test" Dim y(1) As String y=x.Split(",") TextBox1.text=y(3) why "Y" which is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.