Hello, I have a problem to print the whole string in argv[4]. here's my code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int, char[]);
int main(int argc, char *argv[])
{ FILE *in;
int x, y,z;
char a;
if (argc!= 5)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);
}
for (y=0; y<YMAX; y++)
{ for (x=0; x<XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ perror("closing input file");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
a = argv[4][0];

fill(x,y,a);
printScreen();
}
void printScreen(void)
{ int x,y;

for (y=0; y<YMAX; y++)
for (x=0; x<XMAX; x++)
if(screen[x][y]!= EOF)
putchar(screen[x][y]);
else
puts("\n");

}
void fill(int x, int y, char a)
{
if ((screen[x][y] != ' '))
return;

screen[x][y] = a;
fill(x,y+1,a);
fill(x,y-1,a);
fill(x+1,y,a);
fill(x-1,y,a);
}

In my code, i only able to print a character. How shoulld i change my code so that i can print the whole string in argv[4]. If i use argv[4][0], then it will only print the first element of the string in argv[4]

Salem commented: Your idea of indentation sucks. -1

Dani AI

Generated

Each cell in your screen holds exactly one char, so you cannot store the whole string "WOW" inside a single cell. To get the visible result you want you must map characters of the string to many cells — for example cycle through "W", "O", "W", ... as you paint the region. was correct to point out argv[4] should be used as a pointer (char *) and was right that the function prototype and the call must agree. Below is a safe, practical approach: pass the pattern string into the filler and write characters from the string into successive cells while you flood-fill the connected area. This example uses an explicit queue (BFS) to avoid deep recursion.

/* cycle pattern across a connected region starting at (sx,sy) */
void flood_fill_with_pattern(int sx, int sy, const char *pat)
{
    if (!pat || *pat == '\0') return;
    int plen = strlen(pat);
    if (sx < 0 || sx >= XMAX || sy < 0 || sy >= YMAX) return;
    if (screen[sx][sy] != ' ') return;

    int qx[XMAX*YMAX], qy[XMAX*YMAX];
    char seen[XMAX][YMAX] = {0};
    int head = 0, tail = 0, idx = 0;

    seen[sx][sy] = 1;
    qx[tail] = sx; qy[tail++] = sy;

    while (head < tail) {
        int x = qx[head], y = qy[head++];

        screen[x][y] = pat[(idx++) % plen];

        const int dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0};
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx >= 0 && nx < XMAX && ny >= 0 && ny < YMAX &&
                !seen[nx][ny] && screen[nx][ny] == ' ') {
                seen[nx][ny] = 1;
                qx[tail] = nx; qy[tail++] = ny;
            }
        }
    }
}

Read the 65×32 input reliably (use fgets per row and ignore trailing \n), then call flood_fill_with_pattern(x, y, argv[4]); (pass the pointer, not *argv[4]). Troubleshooting notes: ensure argv[4] is not empty, check your start coordinates are in bounds, use the seen array to avoid revisiting cells, and prefer the queue approach above to prevent stack overflow for large fills.

Recommended Answers

All 10 Replies

variable a should be declared as char*

char* a;

variable a should be declared as char*

char* a;

If u don't mind could you please explain more. I'm not good in pointer. Which char a should i change? Is it in function prototype or i should change all of them? OK. Thanks

argv is a two-diminsional array of strings. char c is only one character, not a pointer to a character array. you need to declare it as char* a in the 3d line following the declaration of main().

argv is a two-diminsional array of strings. char c is only one character, not a pointer to a character array. you need to declare it as char* a in the 3d line following the declaration of main().

int main(int argc, char *argv[])
{ FILE *in;
int x, y;
char *a;
if (argc!= 5)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);
}
/* Read the 65x32 text file */
for (y=0; y<YMAX; y++)
{ for (x=0; x<XMAX; x++)
screen[x][y] = fgetc(in);
/* To bypass the extra newline character */
}
if (fclose(in))
{ perror("closing input file");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
a = argv[4];
 
fill(x,y,*a);
printScreen();
}

I've changed my code like the above but it still print first character of argv[4]. May I know why?

remove the star from the fill function (3d line from the bottom). you should really use [ code=c ] tags so your posted program will have line numbers and you don't have to manually add those color tags.

also, post the function printscreen(), I have no idea what it is.

Also, to add to what AD mentions, format your code so we can read it.

remove the star from the fill function (3d line from the bottom). you should really use [ code=c ] tags so your posted program will have line numbers and you don't have to manually add those color tags.

also, post the function printscreen(), I have no idea what it is.

If i removed the star, the program will give me an error.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define XMAX 65
#define YMAX 32

char screen[XMAX][YMAX];

void printScreen(void);

void fill(int, int, char);

 
int main(int argc, char *argv[])
{
    FILE *in;
    int x, y;
    char *a;
 
   if (argc!= 5)
   {
    fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
    exit(1);
   }
 
   in = fopen(argv[1],"r");
   if (in == NULL)
  { 
      perror("opening input file");
      exit(1);
  }

 
   for (y=0; y<YMAX; y++){
      for (x=0; x<XMAX; x++)
        screen[x][y] = fgetc(in);
  }

 
   if (fclose(in))
  { 
       perror("closing input file"); 
       exit(1);
  }
 
   printScreen();
   x = atoi(argv[2]);
   y = atoi(argv[3]);
   a = argv[4];
 

   fill(x,y,*a);

   printScreen();
}

 
void printScreen(void)
{
     int x,y;
 
    for (y=0; y<YMAX; y++)
     for (x=0; x<XMAX; x++)
       if(screen[x][y]!= EOF)
          putchar(screen[x][y]);
      else
          puts("\n");
 
}

 
void fill(int x, int y, char a)
{
   if ((screen[x][y] != ' '))
     return;
 

   screen[x][y] = a;
   fill(x,y+1,a);
   fill(x,y-1,a);
   fill(x+1,y,a);
   fill(x-1,y,a);
}

you have changed that fill function -- your original code took a character array as the second argument, now the latest code only takes a single character. With this code you need to put that star back in.

Hooo boy....

int main(int argc, char *argv[])
{
...
   fill(x,y,*a);    // remove the *, you will pass in the array

   printScreen();
}

...

void fill(int x, int y, char a)  // Add the *, so the function knows it's a pointer
{
   if ((screen[x][y] != ' '))
     return;
 

   screen[x][y] = a;  // now that it's a pointer, you figure out how to 
   fill(x,y+1,a);     //    do what you want here, especially since you
   fill(x,y-1,a);     //    have no comments explaining what this is
   fill(x+1,y,a);     //    supposed to accomplish.
   fill(x-1,y,a);
}

Actually the output of the program will be like this

[IMG][/IMG]

If we choose I as argv[4] then I will fill up the inner part of the output. What i want to make here is instead of character, I want string to fill up the output like 'WOW'. That's the problem I encountered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.