How do I find the center of each cross?

Scan from left to right then top to bottom.
Remember, a '+' can only be used to from one cross

Contents of sample.txt:
4 8
---+--+-
-+---+++
+++---+-
-+--++--

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
#include <iostream> 
#include <string>
#include <fstream>
char** arrayFromFile(std::string fileName, int& numRows, int& numCols);
void search(char **array,int rows, int cols,std::string fileName);

int main(int argc, char* argv[]){
//terminal input 
std::string fileName;
if(argc>1)
fileName=argv[1];

int numCols=0;
int numRows=0;
char** array=nullptr;

array=arrayFromFile(fileName,numRows,numCols);
for(int i=0;i<numRows;i++){
for(int j=0;j<numCols;j++){
std::cout<<array[i][j];
}
std::cout<<'\n';
}

search(array,numRows,numCols,fileName);




for(int i=0;i<numRows;i++)
delete[] array[i];



delete[] array;

return 0;
}

char** arrayFromFile(std::string fileName, int& numRows, int& numCols){
//read from file
std::ifstream inFile;
inFile.open(fileName);
inFile>>numRows;
inFile>>numCols;

char ** array= new char *[numRows];

for(int i=0;i<numRows;i++)
array[i]=new char[numCols];


for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
inFile>>array[i][j];

return array;
}

void search(char **array,int rows,int cols,std::string fileName){
std::cout<<fileName<<" contains "<<2<<" crosses.";
std::cout<<"Location(s) of center(s):\n";
for(int i=1;i<rows;i++){
for(int j=1;j<cols;j++){
if(array[i][j]=='+'){
std::cout<<"("<<i<<","<<j<<")\n";
}


}
}


}
what do you expect from that sample input?
sample.txt contains 2 crosses.
Location(s) of center(s):
(1,6)
(2,1)
ok...
a center can't be on the edges. if you can write a loop that goes from row 1 to row n-1 and column 1 to column m-1 and prints each element, that is the first step (print to verify its working).

step 2, if the character in the loop above is a +, check the 4 cells to verify yes or no (up one, left one, right one, down one). If all those are +, its a cross center. Its critical that the first loop works, or this one will crash your program when you hit memory off the edges.

step 3, if the char was a center, set all 5 of the above (the center and the 4 checks) to some other character (x, maybe, whatever) so they can't be re-used. If you need the data for something else after this, set all the x's back to +s at the end by looping over every cell again. If you are done with the data, you can leave the damage.

Last edited on
Topic archived. No new replies allowed.