Search code examples
cspiral

Need to write spiral from # and


I have to create spiral from # and .. It has to expand according to variable n. Problem is I couldn't create it anyhow I try. My program so far gets Segmentation fault on test. In description image is output for n = 7.

Can anyone please help?

char a[100][100];
int n = 7;
int r,s;

void write(char *a[], int n)
{
  int i, j;
  for (i=0;i<n;i++)
  {
    for (j=0; j<n;j++)
      a[i][j]=".";
  }
}

void right (int length)
{
  int k=0;
  for (k=0;k<length;k++)
    a[r][s+k] = '#';
}

void down(int length)
{
  int k=0;
  for (k=0;k<length;k++)
    a[r][s+k] = '#';
}

void up(int length)
{
  int k=0;
  for (k=0;k<length;k++)
    a[r][s+k] = '#';
}

void left(int length)
{
  int k=0;
  for (k=0;k<length;k++)
    a[r][s+k] = '#';
}

Solution

  • To answer your question about the segmentation fault:

    As I mentioned in the comments, you need to change "." from string to a char like so '.'.

    Next is that in your write function, the argument requires the wrong kind of array. You are specifying char *a[] when instead you need char a[100][100]. This worked for me.

    Regarding the intended behaviour, it is up to you what you define a spiral to be.