Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用c语言编程打印flloyd';s三角形。程序停止工作_C_Arrays_String_Loops_Io - Fatal编程技术网

用c语言编程打印flloyd';s三角形。程序停止工作

用c语言编程打印flloyd';s三角形。程序停止工作,c,arrays,string,loops,io,C,Arrays,String,Loops,Io,我用c编写了下面的代码来打印floyd的三角形 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);

我用c编写了下面的代码来打印floyd的三角形

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;
}
intmain()
{
printf(“输入您想要的行数”);
int t;
scanf(“%d”、&t);
int i;
字符a[1000]=“”;

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

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;
}
请注意,
999
是数组的大小减去1



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

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

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;
}
请注意,
999
是数组的大小减去1



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

这将为您提供所需的输出:

#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;
}
#包括
#包括
int main()
{
printf(“输入您想要的行数:”;
int t;
scanf(“%d”、&t);

对于(int i=1;i,这应该会提供您想要的输出:

#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;
}
#包括
#包括
int main()
{
printf(“输入您想要的行数:”;
int t;
scanf(“%d”、&t);

对于(int i=1;i您可以先构造字符串(较大的一个),然后在每行中只打印其中的一部分:

#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;
}
#包括
#包括
int main()
{
printf(“输入您想要的行数”);
int t;
scanf(“%d”,&t);//您应该检查返回值。。。
认沽权(“”);
char模式[t+1];//这是一个VLA,因此您需要一个兼容C99的编译器或
//使用你的大阵列。。。
//从最后一行开始初始化字符串(最后一行)
//char(空终止符)并返回到第一行。每行
//以“1”结尾,因此在循环中,从它开始。
int i=t;
模式[i]='\0';
而(i>0)
{
模式[--i]=“1”;
如果(i==0)
打破
模式[--i]=“0”;
}
//仅打印每行中所需的零件
char*tmp=&pattern[t-1];
对于(int i=0;i
您可以先构造字符串(较大的一个),然后在每行中只打印其中的一部分:

#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;
}
#包括
#包括
int main()
{
printf(“输入您想要的行数”);
int t;
scanf(“%d”,&t);//您应该检查返回值。。。
认沽权(“”);
char模式[t+1];//这是一个VLA,因此您需要一个兼容C99的编译器或
//使用你的大阵列。。。
//从最后一行开始初始化字符串(最后一行)
//char(空终止符)并返回到第一行。每行
//以“1”结尾,因此在循环中,从它开始。
int i=t;
模式[i]='\0';
而(i>0)
{
模式[--i]=“1”;
如果(i==0)
打破
模式[--i]=“0”;
}
//仅打印每行中所需的零件
char*tmp=&pattern[t-1];
对于(int i=0;i
此外,您可能希望在
else
语句周围添加大括号;此时,第二个
printf(“%c\n”,a)
将被调用,而不管if-else块是什么。或者您可以删除第一个
printf(“%c\n”,a)
statement。非常感谢您的帮助。程序现在没有停止工作,但我仍然没有得到所需的输出。例如,如果我输入的行数为4。我得到4个空白行,我希望模式类似于-1 01 101 0101 10101,依此类推。每一行中的数字请添加到问题中,以获得预期的输出。我不知道你的意思是什么。我已经在代码中添加了预期的输出。另外,你可能希望在
else
语句周围添加大括号;此时,第二个
printf(“%c\n”,a)
将被调用,而不管if-else块是什么。或者你可以删除第一个
printf(“%c\n”,a)
statement。非常感谢您的帮助。程序现在没有停止工作,但我仍然没有得到所需的输出。例如,如果我输入的行数为4。我得到4个空白行,我希望模式类似于-1 01 101 0101 10101,依此类推。每一行中的数字请添加到问题中,以获得预期的输出。我不知道你的意思是什么?我已经在代码中添加了预期的输出。这不是我期望从程序中得到的输出。我已经在我的代码中提到了预期的输出question@jamesgem向后的
strcat
printf
中的错误格式是程序崩溃的原因。您应该能够找出其余的原因但是我需要用那种方式使用strcat,否则我会得到你想要的输出getting@gsamaras为了简洁起见,您应该删除答案的第一部分(输出不正确)。@jamesgem这种方法将交替数字添加到字符串的末尾,但
printf()
从字符串开始打印,有效地反转了输出。这不是我要的输出