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?
The problem is probably because you never initialize the variable l
. Using an uninitialized variables yields in undefined behaviour (google that).
Also your array indexes start at 1 as for example here:
for (i = 1; i <= n; i++)
In C array indexes start at 0, so you should write this:
for (i = 0; i < n; i++)
This appies to all your other for
loops.
odd
instead of o
and even
instead of e
would make your program much easier to read.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.