954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to sort odd&even numbers in arrays?(turbo c)

i research a code, and it goes like this:

include<stdio.h>
#include<conio.h>
void main()
{
int a[12],b[12],c[12],i,j=0,k=0;
clrscr();
printf("Enter  12  numbers\n");
for(i=0;i<12;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<12;i++)
{
if((a[i]%2)==0)
{
b[j]=a[i];
j++;
}
else
{
c[k]=a[i];
k++;
}
}
printf("The  even numbers  are\n");
for(i=0;i<j;i++)
{
printf("%d\n",b[i]);
}
printf("The odd numbers are\n");
for(i=0;i<k;i++)
{
printf("%d\n",c[i]);
}
getch();
}

~~but it is so confusing. i have edited it many times but the output is just infinity of numbers.. please help =

shaieen
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

try using code tags on future posts heres a slightly edited version, its not much different, but cleaned i cleaned it up a little bit made it a little easier to read. i dont really know anything about conio.h, but i assume clrscr and getch are both found in it. i also doubt thats your issue, but i dont see anything wrong with the loops which is usually why a program will print endless numbers.

EDIT: not sure if this is it but after thinking it through you add to j and k one extra time. so in your print loops do as i do, j-1 and k-1

#include<stdio.h>
#include<conio.h>
void main(){

int a[12],b[12],c[12],i,j=0,k=0;
clrscr();
printf("Enter 12 numbers\n");

for(i=0;i<12;i++){
scanf("%d",&a[i]);

if((a[i]%2)==0){
b[j]=a[i];
j++;
}

else{
c[k]=a[i];
k++;
}
}

printf("The even numbers are\n");

for(i=0;i<j-1;i++){
printf("%d\n",b[i]);
}

printf("The odd numbers are\n");

for(i=0;i<k-1;i++){
printf("%d\n",c[i]);
}
}
Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

i research a code, and it goes like this:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[12],b[12],c[12],i,j=0,k=0;
clrscr();
printf("Enter  12  numbers\n");
for(i=0;i<12;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<12;i++)
{
if((a[i]%2)==0)
{
b[j]=a[i];
j++;
}
else
{
c[k]=a[i];
k++;
}
}
printf("The  even numbers  are\n");
for(i=0;i<j;i++)
{
printf("%d\n",b[i]);
}
printf("The odd numbers are\n");
for(i=0;i<k;i++)
{
printf("%d\n",c[i]);
}
getch();
}

~~but it is so confusing. i have edited it many times but the output is just infinity of numbers.. please help =

The code written by you is perfectly alright(although you have missed out "#" in first statement while putting it up) apart from the fact that you are not using a standard compiler.
The code given by you ran perfectly with the addition of the "#".No other flaws and ya try not to use conio and clrscr and all...

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

wow.. =] i have solved it already.. thanks

shaieen
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

oh and why the hell is this a poll, the poll doesnt even make sense

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 
oh and why the hell is this a poll, the poll doesnt even make sense


~~ hehe.. i dont even know what's a poll. hehe. sorry if i posted it wrong.

shaieen
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You