Starting from:

$30

CS204-Homework 1 Matrix Operations Solved

. You are asked to implement a program which finds counts of words(strings) with given N characters in a 2D matrix. Input  

The program prompts for the input file name. Then, it reads the file name from the standard input. The first line of the input file contains a single integer stating the number of rows in the matrix (M), which is equal to the number of columns because matrices will be square matrix (M x M matrix with M rows and M columns).  

The following lines will be the rows of matrix. All elements in the cells of matrix must be uppercase letters (A-Z) – your program must check this. You also need to check if there are indeed M rows and M columns. There is an empty line after matrix rows. At the following line of empty line, there is a single integer (N) for the length of strings you will be searching in the matrix. A sample input file can be as follows:

4 ABCD 

BCDA 

CDAB 

DABC 



 
 

Program Flow
What your program needs to do is find and list strings with given length N and the number of their appearances in the (M x M) matrix.  

•        The strings can start from any cell in the matrix.  

•        Once you are in a cell, you can go any of the four directions: left, right, up and down.  

•        You must NOT visit the same cell while generating a string.  

•        You will stop once you have N letters in your string (i.e., once you visit N cells).  

Boundaries of variables are as follows: 0 < N < 20 and 0 < M < 20. With that in mind, the flow of the program will be as following:

•        Prompt for the input file name and perform an input check. If an invalid file name is entered, ask again until a correct file name is entered.

•        Read the value of M from the file. Store the matrix in the given input file. If the size of matrix does not satisfy the given row (equal to column) value M, print an error message and end the execution.

•        Read N from input file.

•        Find all the strings of length N in the matrix. Do not forget: while generating strings, the directions to move are left, right, up, down.  

•        Store the strings you have found and the number of times they exist in the matrix.

•        Print out the strings and the number of their appearances. For the output format check sample runs.

Sample output for the sample input given above is as following:

The given matrix includes: 

[+] ABC exists 8 many times. 

[+] BCD exists 8 many times. [+] BAB exists 4 many times. 

[+] BCB exists 4 many times. 

[+] CDA exists 10 many times. 

[+] CBC exists 4 many times. 

[+] CDC exists 4 many times. [+] CBA exists 8 many times. 

[+] DAB exists 8 many times. [+] DCD exists 6 many times. 

[+] DAD exists 6 many times. 

[+] DCB exists 8 many times. 

[+] ADA exists 4 many times. 

[+] ABA exists 4 many times. 

[+] ADC exists 10 many times. 

[+] BAD exists 8 many times. 

Note: 

Reuse of same cell is not allowed while generating strings. The following image is an example to the rule where the size of the strings N is set to 6.  

 

It is not allowed to generate “KQCDCQ” as in the figure since Q at (2,3) is visited twice. However the string “KQCDCQ” is in the matrix: the last Q can come from (4,3). So your program must report only this.

                

 

1.    Sample Runs  
Below you can find sample outputs for the program: 

input.txt 



ABC 

BCDDF 

CDAB 

DABC 

 



 
 

Case 1: 

Please enter the input file name: input1.txt Couldn’t open file. Please enter again: input.txt 

Input file is not as expected. 

Press any key to continue . . . 

 

 

input2.txt 

 



ABCD 

BCDA 

CDAB 

 



 
 

Case 2: 

Please enter the input file name: input.txt 

Input file is not as expected. 

Press any key to continue . . . 

 

Input3.txt 

 



AB* 

BCD 

CDA 

 



 
 

Case 3: 

Please enter the input file name: input3.txt 

Input file is not as expected. 

Press any key to continue . . . 

 

Input4.txt 

 



555 

BCD 

CDA 

 



 
 

Case 4: 

Please enter the input file name: input4.txt 

Input file is not as expected. 

Press any key to continue . . . 

 

Input5.txt 

 



ABCD 

BCDA 

CDAB 

DABC 

 



 
 

Case 5: 

Please enter the input file name: input5.txt 

Given matrix includes: 

[+] ABC exists 8 many times. [+] BCD exists 8 many times. [+] BAB exists 4 many times. [+] BCB exists 4 many times. 

[+] CDA exists 10 many times. 

[+] CBC exists 4 many times. 

[+] CDC exists 4 many times. [+] CBA exists 8 many times. 

[+] DAB exists 8 many times. [+] DCD exists 6 many times. [+] DAD exists 6 many times. 

[+] DCB exists 8 many times. 

[+] ADA exists 4 many times. 

[+] ABA exists 4 many times. 

[+] ADC exists 10 many times. 

[+] BAD exists 8 many times. 

Press any key to continue . . . 

 

More products