简体   繁体   English

使用C中的数组显示模式

[英]Displaying pattern using arrays in C

I am trying to display a pattern in C. It looks like this: 我试图在C中显示一个模式。它看起来像这样:

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): 这是使用常规方式(使用for循环和换行符)显示的内容:

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. 我正在看你的代码,必须说所有的ijk等,真让我感到困惑。 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--; 您是从j=n+i并做k--; after printing, meaning the first letter you are printing is the same as the highest, meaning you'll print "ABCDDCBA"... 打印后,意味着您要打印的第一个字母与最高字母相同,这意味着您将打印“ 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 ,输出是正确的:

ABCDCBA
ABC CBA
AB   BA
A     A.

But for the input of ABCDDCBA , the output is wrong: 但是对于ABCDDCBA的输入,输出是错误的:

ABCDDCBA
ABCD CBA
ABC   BA
AB     A
A

My first thought is: make 2 loops, first one puts into array letters from A..? 我的第一个想法是:进行2个循环,第一个放入A .. and 2 puts into your array letters from ?-1 to A. 和2将您输入的数组字母从?-1到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. 如果您想出一种方法来说明(row,col)处的字符,则填充数组很简单。 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. 由于输出是围绕第3列对称的,因此考虑abs(col - 3)是合乎逻辑的,因为那是到中心列的距离。 Call that d . d Subsequent rows show fewer central columns: my observation is that a column is omitted when d is smaller than the row number. 随后的行显示较少的中心列:我的观察是,当d小于行号时,将省略一列。 Finally, what character to print? 最后,要打印什么字符? Well the central column is 'D' and the letters decrease as you move out from the center. 中心列为“ D”,并且字母从中心移出时会减少。

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. 您也可以像遍历原始代码一样遍历每个字符串并putc字符。

#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. 我将提供此解决方案,该解决方案尊重使用2D阵列的愿望。 As written, it uses a C99 feature — namely a VLA in the bridge() function. 如所写,它使用C99功能-即bridge()函数中的VLA。 (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). (因为输出看起来有点像桥,所以使用了bridge()函数的名称。)您可以使用合适的最大大小来固定数组的大小( char lines[9][18];最多可以确定大小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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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