[英]Write a C program to arrange the numbers in an array as a series of odd and even numbers
I've made an attempt to create the above program,but there's some error.Here's my program 我已经尝试创建上面的程序,但是有一些错误。这是我的程序
#include <stdio.h>
#include <conio.h>
void main()
{
int a[20],o[20],e[20],c[40],i,j,k,l,n;
printf("enter size of array");
scanf("%d",&n);
printf("enter array");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=(n-j);k++)
{
if(a[i]%2==0)
{
o[j]=a[i];
break;
}
else
{
e[k]=a[i];
}
}
}
}
for(i=1;i<=j;i++)
{
c[l]=o[i];
}
for(i=1;i<=k;i++)
{
c[l+k]=e[k];
}
printf("The new array is");
for(l=1;l<=(j+k);l++)
{
printf(" %d ",c[l]);
}
getch();
}
Can anybody please help me rectify the error in the above program?Can someone also give me a few tips on how to become a good C programmer? 有人可以帮我纠正上述程序中的错误吗?有人可以给我一些如何成为一名优秀C程序员的提示吗?
The problem is probably because you never initialize the variable l
. 问题可能是因为您从未初始化变量
l
。 Using an uninitialized variables yields in undefined behaviour (google that). 使用未初始化的变量会产生未定义的行为 (google that)。
Also your array indexes start at 1 as for example here: 同样,您的数组索引从1开始,例如在这里:
for (i = 1; i <= n; i++)
In C array indexes start at 0, so you should write this: 在C数组中,索引从0开始,因此您应该这样写:
for (i = 0; i < n; i++)
This appies to all your other for
loops. 这适用于所有其他
for
循环。
odd
instead of o
and even
instead of e
would make your program much easier to read. odd
而不是o
和even
而不是e
等变量名将使程序更易于阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.