C 为什么这段代码适用于1和2,但对于大于3的输入却失败了?

C 为什么这段代码适用于1和2,但对于大于3的输入却失败了?,c,nested-loops,C,Nested Loops,所以我试着用C语言打印图案 For n = 2 Output: 2 2 2 2 1 2 2 2 2 for n = 3 Output: 3 3 3 3 3 3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3 and so on. 我的代码: #include <stdio.h> #include <stdlib.h> int main() { int n; scanf("%d",&n);

所以我试着用C语言打印图案

For n = 2
Output:
2 2 2
2 1 2
2 2 2
for n = 3
Output:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3 
and so on.
我的代码:

#include <stdio.h>
#include <stdlib.h>
    
int main()
{
    int n;
    scanf("%d",&n);
    int nn = n;
    int *arr;
    arr = (int*)malloc(n*sizeof(int));
    int f = 0; //flag to check if I reached the mid of the pattern
    int l = 2*n-1; //Lenght of square to be generated
    int temp1 = 0;
    int temp2 = l;
    for(int i = 0;i<l;i++)
    {
        for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
        {
            arr[j] = n;
        }
        for(int k = 0;k<l;k++)
        {
            printf("%d ",arr[k]);
        }
        printf("\n");
        if(n == 1)
        {
            f = 1;
        }
        if(f==0)        //For upper half of pattern
        {
            n=n-1;
            temp1=temp1+1;
            temp2=temp2-1;
        }
        else if(f==1) //For lower half of pattern
        {
            n=n+1;
            temp1=temp1-1;
            temp2=temp2+1;
        }
    }
    return(0);
}
#包括
#包括
int main()
{
int n;
scanf(“%d”和“&n”);
int nn=n;
int*arr;
arr=(int*)malloc(n*sizeof(int));
int f=0;//检查我是否到达模式中间的标志
int l=2*n-1;//要生成的正方形长度
int temp1=0;
int temp2=l;
对于(int i=0;i,在该代码段中


对于(int j=temp1;j最好按轴检查距离,并取最大值进行打印

比如:

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

int main()
{
    int vertical_distance;
    int horizontal_distance;
    
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n * 2 + 1; i++)
    {
        for(int j = 0; j < n * 2 + 1; j++)
        {
            vertical_distance   = abs(n - i);
            horizontal_distance = abs(n - j);
            if (vertical_distance > horizontal_distance)
                printf("%d ", vertical_distance);
            else
                printf("%d ", horizontal_distance);
        }
        printf("\n");
    }
    return(0);
}
#包括
#包括
int main()
{
int垂直距离;
int水平距离;
int n;
scanf(“%d”和“&n”);
对于(int i=0;i水平距离)
printf(“%d”,垂直距离);
其他的
printf(“%d”,水平距离);
}
printf(“\n”);
}
返回(0);
}
另外,当我运行代码时,它可以很好地处理大量的数字(3、7、15)。 我刚刚将你的代码粘贴到onlinegdb.com/online_c_compiler并运行了它。你能添加你收到的错误消息吗


(sry代表我的英语)

你能找到哪一行触发了崩溃吗?那一行中出现的变量值是什么?首先,你有没有尝试用钢笔和纸来解决它?然后你有没有尝试使用调试器在监视变量及其值的同时一步一步地检查代码语句(为了确保你的代码做了它应该做的事情,并且它遵循了你用笔和纸提出的方法)?提示:你为
arr
分配了多少元素?你实际使用了该数组中的多少元素(例如在循环
中)(int j=temp1;j1首先查看您需要的内存分配(n+1)*(n+1)个位置。@JaMiT它表明igoe.exe已停止工作。返回255感谢您指出这一点!实际可用于(2*n-1)。数组大小分配错误,因此程序就像.exe文件停止工作一样崩溃。这是一个更优雅的解决方案。谢谢。PS:你的英语很好。
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int vertical_distance;
    int horizontal_distance;
    
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n * 2 + 1; i++)
    {
        for(int j = 0; j < n * 2 + 1; j++)
        {
            vertical_distance   = abs(n - i);
            horizontal_distance = abs(n - j);
            if (vertical_distance > horizontal_distance)
                printf("%d ", vertical_distance);
            else
                printf("%d ", horizontal_distance);
        }
        printf("\n");
    }
    return(0);
}