in c programming only How take an integer (size) and a filename as its two comma
ID: 3766448 • Letter: I
Question
in c programming only How take an integer (size) and a filename as its two command-line arguments. The size represents the number of rows and columns in a square matrix of characters. The filename specified contains the actual square matrix of these characters.On the command line, you would have specified 6 and data1.txt as your two command-line arguments. When your program prompts for a word to find, imagine the user enters bread.
Your program should find all the occurrences of bread and report this to the user, as shown below:
Explanation / Answer
// running out of time. Will update answer with commentary in 10 mins
#include <stdio.h>
typedef struct
{
int x;
int y;
} pair;
void search_word(char *matrix[], int size, char *word)
{
int i, j, k, p;
int LtoR = 0, TtoB = 0, BtoT = 0, RtoL = 0;
int len = strlen(word);
pair *LtoR_pairs = (pair*)malloc(sizeof(pair)*LtoR);
pair *TtoB_pairs = (pair*)malloc(sizeof(pair)*TtoB);
pair *BtoT_pairs = (pair*)malloc(sizeof(pair)*BtoT);
pair *RtoL_pairs = (pair*)malloc(sizeof(pair)*RtoL);
pair temp;
k = 0;
for(i = 0; i < size; i++)
{
for(j = 0; j <= size-len+k; j++)
{
if(matrix[i][j] == word[k])
{
k++;
if(k == len)
{
LtoR++;
LtoR_pairs = (pair*) realloc(LtoR_pairs, sizeof(pair)*LtoR);
LtoR_pairs[LtoR-1].x = i;
LtoR_pairs[LtoR-1].y = j-k+1;
k = 0;
}
}
}
k = 0;
}
k = 0;
for(i = 0; i < size; i++)
{
for(j = 0; j <= size-len+k; j++)
{
if(matrix[j][i] == word[k])
{
k++;
if(k == len)
{
TtoB++;
TtoB_pairs = (pair*) realloc(TtoB_pairs, sizeof(pair)*TtoB);
TtoB_pairs[TtoB-1].x = j-k+1;
TtoB_pairs[TtoB-1].y = i;
k = 0;
}
}
}
k = 0;
}
k = len;
for(i = 0; i < size; i++)
{
for(j = 0; j <= size-k; j++)
{
if(matrix[j][i] == word[k-1])
{
k--;
if(k == 0)
{
BtoT++;
BtoT_pairs = (pair*) realloc(BtoT_pairs, sizeof(pair)*BtoT);
BtoT_pairs[BtoT-1].x = j-len+1;
BtoT_pairs[BtoT-1].y = i;
k = len;
}
}
}
k = len;
}
k = len;
for(i = 0; i < size; i++)
{
for(j = 0; j <= size-k; j++)
{
if(matrix[i][j] == word[k-1])
{
k--;
if(k == 0)
{
RtoL++;
RtoL_pairs = (pair*) realloc(RtoL_pairs, sizeof(pair)*RtoL);
RtoL_pairs[RtoL-1].x = i;
RtoL_pairs[RtoL-1].y = j-len+1;
k = len;
}
}
}
k = len;
}
printf("Tee word %s was found %d times ",word,LtoR+TtoB+BtoT+RtoL);
printf(" %d times written Left-to-Right ",LtoR);
printf(" %d times written Top-to-Bottom ",TtoB);
printf(" %d times written Bottom-to-Top ",BtoT);
printf(" %d times written Right-to-Left ",RtoL);
printf("It exists at the following places: ");
if(LtoR > 0)
for(i = 0; i <size; i++)
{
for(j=0;j<size;j++)
{
for(p = 0; p < LtoR; p++)
if(i == LtoR_pairs[k].x && j == LtoR_pairs[k].y)
{
for(k=j;k<len;k++)
printf("%c",matrix[i][k]);
j = k-1;
break;
}
if(p == LtoR)
printf("%c",matrix[i][j]);
}
printf(" ");
}
if(RtoL > 0)
for(i = 0; i <size; i++)
{
for(j=0;j<size;j++)
{
k = 0;
for(p = 0; p < RtoL; p++)
if(i == RtoL_pairs[k].x && j == RtoL_pairs[k].y)
{
for(k=j;k<len;k++)
printf("%c",matrix[i][k]);
j = k-1;
break;
}
if(p == RtoL)
printf("%c",matrix[i][j]);
}
printf(" ");
}
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Provide size of matrix and file name as command line arguments ");
return 0;
}
int size = atoi(argv[1]);
char *filename = argv[2];
char *matrix[size];
char *word;
int i;
FILE *fp = fopen(filename,"r");
if(fp==NULL)
{
printf("ERROR : Not able to open Matrix file ");
return 0;
}
for(i = 0; i<size; i++)
{
matrix[i] = malloc(7);
fscanf(fp,"%s",matrix[i]);
}
while(1)
{
printf("Enter a word to find in the puzzle or 'zzz' to exit : ");
scanf("%s", word);
if(strcmp(word,"zzz")==0)
break;
if(strlen(word) > size)
{
printf("The word entered to find in the puzzle is bigger than the size of the puzzle. ");
continue;
}
search_word(matrix, size, word);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.