im trying to sort my random numbers to odd numbers and even numbers,,

the last part of my code is really wrong.....(the sorting part)

``````my code:
#include<time.h>

int main()
{
int num[50];
int x,odd,even;
srand(time(NULL));

for(x=0; x<50; x++)
{
num[x] = (rand()%60)+40;

}

for(x = 49; x>-1; x--)
{
[B]if(num[x] % 1==0)
{
num[x]=odd;
}
if(num[x] % 2==0)
{
num[x] = even;
}

printf("%d\n", odd);
printf("%d\n", even);[/B]
}
getch();
return;
}``````
4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by ssharish2005

Here is a little help for the last part

``````for(x = 49; x>-1; x--)
{
if(num[x] % 2==0)
{
printf ("%i is even\n", num[x]);
}
else
{
printf ("%i is odd\n", num[x]);
}
}``````

Since it helps to explain what is wrong instead of just tossing new unexplained code at you, here's what's wrong with your code:

``````my code:
#include<time.h>

int main()
{
// first of all, why isn't the following indented?
int num[50];
int x,odd,even;
srand(time(NULL));

for(x=0; x<50; x++)
{
num[x] = (rand()%60)+40;

}
// second, why is all the following indented?
for(x = 49; x>-1; x--)
{
if(num[x] % 1==0)    // x % 1 is ALWAYS 0.  Any number / 1
// has a remainder of 0.  You need
// x %2
{
num[x]=odd;          // 'odd' has no value, it never was
// set. Do you really want to
// replace the number with 'odd'?
}
if(num[x] % 2==0)    // This next section would be better
// served with an 'else'
{
num[x] = even;
}

printf("%d\n", odd);
printf("%d\n", even);
}
getch();
return;
}``````

See this about code formatting. Also explain what you are trying to do. "sort" has many definitions, and although what you seem to be doing can be called a sort, it's not what we consider a sort.

im trying to sort my random numbers to odd numbers and even numbers,,

What u wanted to sort on both odd and even in the same array. Stars all over head lol

You can sort either on odd or even thats it. Perhaps u can have two array one sorted with even and one sorted with odd.

And dont use getch() function(instead use getchar() ) its a non standard function and main should return 0.

As people said u need to work around on your code indentation.

ssharish

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.