Hi All, I wanna to search a pattern in a txt file called A which is a 2D matrix from another file called B. The requirment is that I need to output the number of matched pattern in file B. But I encounted difficulty of counting the number.
Basically I am trying to input to matrix from A to a tmp_matrix, and then I am using for loop to search the pattern char by char.
I am too new to know that if there is other existing function can do such job. Can you suggest a bit too me, or help to look at the code below. Thank you so much.
#include<stdio.h> //Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<math.h>
using namespace std;
#define MAXROWS 100 //Define max rows =10
#define MAXCOLS 100 //Define max cols =10
void readinput(int in[][MAXCOLS]); //Function prototype
//void writeoutputA(int out[][MAXCOLS]);
//void writeoutputB(int count);
void main()
{
int random_matrix[MAXROWS][MAXCOLS];
int tmp_matrix[MAXROWS][MAXCOLS];
int i,j;
int count = 0;
{
FILE *inputFile=fopen("Patterns.txt","r");
for(int row=0; row<2; row++) /* to keep on reading every number */
{
for(int col=0; col<2; col++)
{
fscanf(inputFile,"%d",&tmp_matrix[row][col]);
}
}
fclose(inputFile); /*Close the file stream*/
}
srand((unsigned)(time(NULL))); /* initialize random seed: */
for (i=0;i<MAXROWS;i++) /* generate random number: */
{
for (j=0;j<MAXCOLS;j++)
{
random_matrix[i][j]= rand() % 2;
}
}
// {
// writeoutputA(random_matrix);
// }
printf ("Random matrix generated successfully, presee [ENTER] to continues...");
getchar();
int result;
for (i=0;i<MAXROWS;i++)
{
for (j=0;j<MAXCOLS;j++)
{
for (int x=0; x<3; x++)
{
for (int y=0; y<3; y++)
{
if (tmp_matrix[x][y] == random_matrix[i+x][j+y])
continue;
else
{
break;
}
}
}
//writeoutputB(count);
}
}
printf("number of pattern found is %d\n\n",count);
system("pause");
}