分配偶数和奇数的数组

时间:2019-02-11 02:43:55

标签: c

我在考虑一个数组,以查找如何在两个连续的数组中分配偶数和奇数。

$ go build -gcflags "-m -l"
# concurrentHTTP/stackoverflow
./pipeline.go:7:5: func literal escapes to heap
./pipeline.go:7:5: func literal escapes to heap
./pipeline.go:6:25: make(chan int) escapes to heap
./pipeline.go:17:5: func literal escapes to heap
./pipeline.go:17:5: func literal escapes to heap
./pipeline.go:16:21: leaking param: in
./pipeline.go:16:36: leaking param: completionFlag
./pipeline.go:19:16: "Received from stage 1 channel " escapes to heap
./pipeline.go:19:16: n escapes to heap
./pipeline.go:19:15: createStageTwo.func1 ... argument does not escape
./pipeline.go:29:14: make(chan struct {}) escapes to heap

程序无法运行。我现在无法发现错误。谁能发现错误?编译器显示 **分段故障

...程序在退出时完成

de 139

按ENTER退出控制台。

**

1 个答案:

答案 0 :(得分:1)

我试图通过使用注释指出一些代码错误。

而且我认为这段代码还有一个逻辑问题,就是您想将一个数字列表放入奇数数组和偶数数组,但是偶数和奇数数组的初始值全为零。

将数字列表划分为两个数组时,不会覆盖所有初始值,这将导致最后打印一些零值,但它们也不属于奇数。

    #include <stdio.h>
    #include <math.h>

        int main()
        {
        int a[100],odd[100],even[100],i,j,n;
    //you should first assign a vaule to n and then use n
    //and be careful for the value of the n,
    //it should not larger than the size of the array,or it will overflow.
        for(i=0;i<=n;i++)
        {
        a[i]=0;
        odd[i]=0;
        even[i]=0;
        }
    //put below two lines to the front of the above for-loop
        printf("Enter the number of elements which you want to check as even or odd");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
        if(a[i]%2==0)
        a[i]=even[i];
        else
        a[i]=odd[i];
        }
        for(i=0;i<n;i++)
    //you type a wrong 'printf' satement,here is the right way
    //printf("The set of even numbers is %d",even[i]);
        printf("The set of even numbers is %d"),even[i];
        for(i=0;i<n;i++)
        printf("The set of even numbers is %d"),odd[i];//you type a wrong 'printf' satement
        return 0;
        }