简体   繁体   中英

Displaying pattern using arrays in C

I am trying to display a pattern in C. It looks like this:

Target:

ABCDCBA
ABC CBA
AB   BA
A     A

I know how to get approximately the same output using a more conventional approach for patterns. This is what is displayed using the conventional way (using for-loops and newline characters):

Getting output:

ABCDDCBA
ABC  CBA
AB    BA
A      A

I want to implement the exact target pattern using arrays instead of the conventional way. I am trying to store everything in an array and then just display the array elements.

This is my code:

#include<stdio.h>
#include<conio.h>

void main()
{
    int k,n=6,m;
    int i=0,j=0;
    int arr[10][10];
    clrscr();
    while(i<=n)
    {
        j=0;
        k=65;
        m=2*n;
        while(j<=m)
        {    
            while(j<=n-i)
            {
                arr[i][j]=k;
                k++;
                j++;
            }
            for(j=n-i;j<=n+i;j++)
            {
                printf(" ");
            }
            for(j=n+i;j<=m;j++)
            {
                printf("%c",k);
                k--;
            }
        }
        i++;
    }
    i=0;
    while(i<=n)
    {
        j=0;
        while(j<=m)
        {
            printf("%c",arr[i][j]);
            j++;

        }
        printf("\n");
        i++;
    }

    getch();
}

i'm looking at your acode and must say all the i , j , k etc. really confuse me. there is a reason you are told to have meaningful variable names, because it's easier to read the code that way and to understand what's the meaning of every one of them. i'm pretty sure that once you'll change the veritable names you'll find your mistake in no time.

having said that, look at your code at

        for(j=n+i;j<=m;j++)
        {
            printf("%c",k);
            k--;
        }

you are stating from j=n+i and do k--; after printing, meaning the first letter you are printing is the same as the highest, meaning you'll print "ABCDDCBA"...

void print(const char* pStr)
{
    if(pStr == NULL)
        return;

    int len = strlen(pStr);
    printf("%s\n", pStr);
    int mid = len / 2 ;
    for (int i=1; i<= mid; i++)
    {
        for (int j =0; j<len; j++)
        {
            if ((j >= (mid-i+1)) && (j <= (mid+i-1)))
                printf(" ");
            else
                printf("%c", pStr[j]);
        }
        printf("\n");
    }
}

This is my answser, for the input ABCDCBA , the output is right:

ABCDCBA
ABC CBA
AB   BA
A     A.

But for the input of ABCDDCBA , the output is wrong:

ABCDDCBA
ABCD CBA
ABC   BA
AB     A
A

My first thought is: make 2 loops, first one puts into array letters from A..? and 2 puts into your array letters from ?-1 to A.

#include <stdio.h>

int main(void)
{
  int no_ofrows;
  char array[26][51];
  int row_no;
  int index;
  int i,j;

  printf("No of rows: ");
  scanf("%d", &no_ofrows);

  for(row_no=0;row_no<no_ofrows;row_no++)
  {
    for(index=0;index<no_ofrows;index++)
    {
      if(index<no_ofrows-row_no)
        array[row_no][index]='A'+index;
      else
        array[row_no][index]=' ';
    }
    for(index=0;index<no_ofrows-1;index++)
    {
      if(index<row_no-1)
        array[row_no][no_ofrows+index]=' ';
      else
        array[row_no][no_ofrows+index]='A' + no_ofrows-2 - index;
    }
  }

  for(i=0;i<no_ofrows;i++)
  {
    for(j=0;j<no_ofrows*2-1;j++)
      printf("%c", array[i][j]);
    putchar('\n');
  }


  return 0;
}

And you're done.

Populating the array is simple, if you figure out a way to say what the character at (row, col) is. Since the output is symmetric around column 3, it's logical to consider abs(col - 3) , since that's the distance from the central column. Call that d . Subsequent rows show fewer central columns: my observation is that a column is omitted when d is smaller than the row number. Finally, what character to print? Well the central column is 'D' and the letters decrease as you move out from the center.

Putting that together, you get a few lines of code that populates the array. I've followed it with a loop that prints the array constructed as strings. You could just as well iterate over each string and putc the characters like the original code did.

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

int main(int argc, char *argv[]) {
    char arr[4][8] = {'\0'};
    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 7; col++) {
            int d = abs(col - 3);
            arr[row][col] = d < row ? ' ' : 'D' - d;
        }
    }
    for (int row = 0; row < 4; row++) {
        puts(arr[row]);
    }
    return 0;
}

I'll proffer this solution, which respects the desire to use a 2D array. As written, it uses a C99 feature — namely a VLA in the bridge() function. (The bridge() function is so named since the output looks a little like a bridge.) You can fix the size of the array with a suitable maximum size ( char lines[9][18]; would do for up to size 9).

#include <stdio.h>

enum { FirstLetter = 'A' };

static void bridge(int n)
{
    int len = n*2 - 1;
    char lines[n][len+1];

    for (int i = 0; i < n; i++)
    {
        char c = FirstLetter;
        char LastLetter = c + n - i - 1;
        for (int j = 0; j < n; j++)
        {
            lines[i][len-1-j] = c;
            lines[i][j] = c;
            if (c >= FirstLetter && c < LastLetter)
                c++;
            else
                c = ' ';
        }
        lines[i][len] = '\0';
    }
    for (int i = 0; i < n; i++)
        printf("%s\n", lines[i]);
}

int main(void)
{
    for (int i = 1; i < 10; i++)
        bridge(i);
    return 0;
}

Sample output:

A
ABA
A A
ABCBA
AB BA
A   A
ABCDCBA
ABC CBA
AB   BA
A     A
ABCDEDCBA
ABCD DCBA
ABC   CBA
AB     BA
A       A
ABCDEFEDCBA
ABCDE EDCBA
ABCD   DCBA
ABC     CBA
AB       BA
A         A
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE   EDCBA
ABCD     DCBA
ABC       CBA
AB         BA
A           A
ABCDEFGHGFEDCBA
ABCDEFG GFEDCBA
ABCDEF   FEDCBA
ABCDE     EDCBA
ABCD       DCBA
ABC         CBA
AB           BA
A             A
ABCDEFGHIHGFEDCBA
ABCDEFGH HGFEDCBA
ABCDEFG   GFEDCBA
ABCDEF     FEDCBA
ABCDE       EDCBA
ABCD         DCBA
ABC           CBA
AB             BA
A               A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM