In this question, firstly, you have to write two functions:
new_array (char** a, int n, int m): create a two-dimension matrix of characters whose size is m*n.
del_array (char** a, int n, int m): delete a two-dimension matrix of characters whose size is m*n.
After that, you use two above functions to perform the following task:
You are given a big image with size M*N
and some small images with size m*n
. Each image is presented by a matrix of characters with its size. Your task is finding the number of positions which each small image occurs in that big image.
Input file: image.inp
.
The first line of the input file contains two positive integers M and N which are respectively the height and the width of the big image.
Each line 2
...M+1
consists of N
characters (a
...z
, A
...Z
) which describe a row of the big image.
Subsequently, there are some small images which you must find the big image. Each small image is written in the formation of the big image. Specially, there is a line having m
= 0 and n
= 0, you have to end your finding process.
Output file: image.out
.
For each small image in the input file, you must write a number which presents the number of positions which that small image occurs in the big image.
image.inp image.out
4 4 3
Aaaa 1
Aaaa
Aaab
Aaaa
2 2
Aa
Aa
2 2
aa
ab
0 0
I did this:
file header:
image.h
:#ifndef _IMAGE_H_ #define _IMAGE_H_ using namespace std; void new_array (char** , int , int ); void del_array (char** , int , ); bool small_image(char**,char**,int,int,int,int) int count_small_image(char** , char** , int ,int ,int ,int ); #endif
file
image.cpp
:#include<iostream> #include "image.h" #include <fstream> using namespace std; void new_array(char** a, int n,int m)
{ ifstream inStream; inStream.open("image.inp");
a=new char* [m] ; for(int i=0; i<m; i++) { a[i]=new char[n]; for(int j=0;j<n; j++) inStream>>a[i][j]; } } void del_array(char** a,int m) { for(int i=0;i<m ;i++) { delete [] a[i]; } delete [] a; } bool small_image(char** a,char** b, int i,int j,int p,int q) { for(int u=i;u<i+p;u++ ) { for(int v=j;v<j+q;v++ ) { if(a[u][v]!=b[u-i][v-j]) return false; } } return true; } int count_small_image(char** a,char** b,int m,int n,int p, int q) { int COUNT=0; for(int i=0;i<m;i++ ) for(int j=0;j<n;j++ ) { if(a[i][j]==b[0][0]) { if((m-i+1)>=p && (n-j+1)>=q) { if(small_image(a,b,i,j,p,q)==false) break; else COUNT++; } } } return COUNT; }
file
main_count_small_image.cpp
:#include <iostream> #include "image.h" #include <fstream> using namespace std; int main() { ifstream inStream; inStream.open("image.inp"); ofstream outStream; outStream.open("image.out"); int m,n,p,q; char** a; char** b; inStream>>n>>m; new_array(a,n,m); inStream>>q>>p; new_array(b,q,p); int c; c=count_small_image(a,b,m,n,p,q); outStream<<c; del_array(a,m); del_array(b,p); return 0; getchar(); }
But, I get:
[error]: has stopped working ...