Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are asked to implement a program that perform horizontal, vertical, and diag

ID: 3780053 • Letter: Y

Question

You are asked to implement a program that perform horizontal, vertical, and diagonal search. The 2D char array can be defined and initialized in the program o read from a file If user executes your program with f filename .txt flag in command line arguments then dynamically create 2D array and read the characters from the given file. The file will first contains 2 integers to represent the number of Rows and the number of Columns. Then it will contain Row Column many characters as follows: 3 4 abcd dcba xyzd. If user executes your program without f filename. txt fl then use the default 2D ag, array in the program. Here is an example: your prog.c fine ROW 3 these numbers will be larger #de #define COL 4 in an actual program main char g [ROW] [COL] a 'b', c', d' 'd', 'c', 'b', a 'x', y 'z', 'd' The words that a user wants to search will be given as command line arguments. User can give as many words as he/she wants. For example, yourprogr bcd bd cy abcdef your prog bcd bd cy abcdef f filename.txt

Explanation / Answer

C Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ROW 3 // change according to your need
#define COL 4 // Change according to your need
int horizontal(char a[],char *G,int,int);
int vertical(char a[],char *G,int,int);
int diagonal(char a[],char *G,int,int);

int main(int argc, char *argv[])
{
int R,C,i,j,find,H;
char g[ROW][COL] = {{'a','b','c','d'},{'d','c','b','a'},{'x','y','z','d'}};
char *G = (char *)malloc(ROW * COL * sizeof(char));
if(!strcmp(argv[argc-2],"-f"))
{
FILE *ptr;
ptr = fopen (argv[argc-1], "r");
H = argc-2;
fscanf(ptr,"%d%d ",&R,&C);
for(i = 0;i<R;i++)
{
for(j = 0;j<C;j++)
{
fscanf(ptr,"%c",G+i*C+j);
}
fscanf(ptr," ");
}
}
else
{
R = ROW;
C = COL;
H = argc;
for(i=0;i<R;i++)
for(j=0;j<C;j++)
*(G+i*C+j) = g[i][j];
}
for(i=1;i<H;i++){
find = horizontal(argv[i],G,R,C);
if(find) find = vertical(argv[i],G,R,C);
if(find) find = diagonal(argv[i],G,R,C);
if(find) printf("%s does not appear in g ",argv[i]);
}
return 0;
}
int horizontal(char a[],char *G,int R,int C)
{
int i,j,k,t;
for(i=0;i<R;i++)
{
for(k=0,j=0;a[k]!=''&&j<C;j++)
{
if(a[k]==*(G+i*C+j))
{
k++;
t = 1;
}
else
{
k = 0;
if(t == 1){j = j-1; t =0;}
}
if(a[k]=='')
{
printf("%s appears horizontally starting at g[%d][%d] ",a,i,j-k+1);
return 0;
}
}
}
return 1;
}
int vertical(char a[],char *G,int R,int C)
{
int i,j,k,t;
for(i=0;i<C;i++)
{
for(k=0,j=0;a[k]!=''&&j<R;j++)
{
if(a[k]==*(G+i+j*C))
{
k++;
t = 1;
}
else
{
k = 0;
if(t == 1){j = j-1; t =0;}
}
if(a[k]=='')
{
printf("%s appears vertically starting at g[%d][%d] ",a,j-k+1,i);
return 0;
}
}
}
return 1;
}
int diagonal(char a[],char *G,int R,int C)
{
int i,j,k,t,l,v;
char temp[R][C];
for(i=0;i<R;i++)
for(j=0;j<C;j++)
temp[i][j]=*(G+i*C+j);

for(i=0;i<R;i++)
for(j=0;j<C;j++)
for(k=0,v=i,l=j;a[k]!=''&&v<R&&l<C;v++,l++)
{
if(a[k]==temp[v][l])
{
k++;
t = 1;
}
else
{
k = 0;
if(t == 1){v--;l--; t =0;}
}
if(a[k]=='')
{
printf("%s appears diagonaly starting at g[%d][%d] ",a,v-k+1,l-k+1);
return 0;
}
}
return 1;
}

OUTPUT

sh-4.2$ main bcd bd cy abcdef                                                                                                                                                   

bcd appears horizontally starting at g[0][1]                                                                                                                                    

bd appears diagonaly starting at g[1][2]                                                                                                                                        

cy appears vertically starting at g[1][1]                                                                                                                                       

abcdef does not appear in g

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote