【问题标题】:Program in c to print flloyd's triangle . Program stops working用 c 编写程序打印弗洛伊德三角形。程序停止工作
【发布时间】:2018-03-19 06:50:43
【问题描述】:

我用c写了下面的代码来打印弗洛伊德的三角形。

int main()
{
    printf("Enter the number of rows you want to have");
    int t;
    scanf("%d",&t);
    int i;
    char a[1000] ="";
    for(i=1;i<=t;i++)
    {
        if (i%2!=0)
        {
            strcat("1",a);
            printf("%c\n",a);}
        else
            strcat("0",a);
        printf("%c\n",a);


    }
    return 0;
}

该程序对我来说似乎很好,但一旦我执行它就停止工作。请帮忙 我希望输出如下-
1
01
101
0101
10101
等等

【问题讨论】:

  • 另外,您可能希望在 else 语句周围添加大括号;目前,无论 if-else 块如何,都会调用第二个 printf("%c\n",a)。或者您可以删除第一个 printf("%c\n",a) 语句。
  • 非常感谢您的帮助。该程序现在并没有停止工作,但我仍然没有得到想要的输出。例如,如果我输入的行数为 4。我得到 4 个空白行
  • 我希望模式像 - 1 01 101 0101 10101 等等。并且每个数字都在一个新行中
  • 请在问题中添加您的预期输出。我不知道Flloyd's triangle 是什么意思。
  • 我在代码中添加了预期的输出

标签: c arrays string loops io


【解决方案1】:

您可以先构造字符串(较大的字符串),然后在每行中仅打印其中的一部分:

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

int main()
{
    printf("Enter the number of rows you want to have");
    int t;
    scanf("%d",&t); // You should check the return value...
    puts("");

    char pattern[t + 1]; // It's a VLA, so you need a C99 compliant compiler or
                         // use your big array...

    // Initialize the string (it's the last row) starting from the last 
    // char (the null terminator) and stepping back to first. Every row should
    // end with a '1', so in the loop, start with it.
    int i = t;
    pattern[i] = '\0';
    while ( i > 0 )
    {
        pattern[--i] = '1';
        if ( i == 0 )
            break;
        pattern[--i] = '0';
    }

    // Print only the part needed in each row
    char* tmp = &pattern[t - 1];
    for ( int i = 0; i < t; ++i, --tmp )
    {
        printf("%s\n", tmp);
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    在启用警告的情况下编译,您将很快看到您需要使用%s(字符串格式)打印a,而不是%c(字符格式)。当我编译你的代码时,我得到了:

    prog.c: In function 'main':
    prog.c:16:22: warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]
                 printf("%c\n",a);
                         ~^    ~
                         %s
    prog.c:20:22: warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]
                 printf("%c\n",a);
                         ~^    ~
                         %s
    

    此外,您的 else 语句缺少花括号,这导致只有 strcat() 被假定为它的主体。

    要获得所需的输出,您应该放弃strcat() 并索引您要分配位的位置,如下所示:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        printf("Enter the number of rows you want to have\n");
        int t;
        scanf("%d",&t);
        int i;
        char a[1000] ="";
        for(i=1;i<=t;i++)
        {
            if (i%2!=0)
            {
                a[999 - i] = '1';
                printf("%s\n", &a[999 - i]);
            }
            else {
                a[999 - i] = '0';
                printf("%s\n", &a[999 - i]);
            }
        }
        return 0;
    }
    

    输出:

    Enter the number of rows you want to have
    4
    1
    01
    101
    0101
    

    请注意,999 是数组的大小,减去 1。


    PS:在您发布的代码中:连接字符串时,您弄乱了参数的顺序。

    【讨论】:

    • 这不是我期望的程序输出。我在我的问题中提到了预期的输出
    • @jamesgem 倒退的strcatprintf 中的错误格式是您的程序崩溃的原因。你应该能够自己解决剩下的问题。
    • 但我需要以这种方式使用 strcat,否则我会得到你得到的输出
    • @gsamaras 为了简洁起见,您应该删除答案的第一部分(输出不正确)。
    • @jamesgem 这种方法将交替的数字添加到字符串的末尾,但 printf() 从字符串的开头打印,有效地反转输出。
    【解决方案3】:

    这应该会给你你正在寻找的输出:

    #include <iostream>
    #include <string.h>
    
    int main()
    {
        printf("Enter the number of rows you want to have: ");
        int t;
        scanf("%d", &t);
    
        for (int i = 1; i <= t; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                char a[1000] = "";
    
                if ((i+j) % 2 == 0)
                    strcat(a, "1");
                else
                    strcat(a, "0");
    
                printf("%s", a);
            }
    
            printf("\n");
        }
    
        return 0;
    }
    

    由于每隔一行都以 0 开头,您可以简单地为每行重新创建字符串 a

    【讨论】:

    • 我们为什么要运行两个循环?
    • 外循环确定当前行上的字符串应该有多长,而内循环用交替的1s 和0s 填充字符串。通过检查(i+j) % 2,您可以确定当前行应该以1 还是0 开头。否则,如果您总是检查 i % 2,那么您将始终以每行相同的数字开头。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多