# Thread: Printing out odd and even values of an array

1. ## Printing out odd and even values of an array

Hi guys, I am trying to print the odd and even values of an array 10 per row. I keep getting the same number printing out can anyone see if my logic is wrong for identifying the odd and even values? Thank you.
Code:
``` int array[1000] = {0};
int i = 0;
int j = 0;
int k = 0;

for(i = 0; i<1000; i++)
{
array[i] = rand();
}
i =0;

for(j = 0; j<100; j++)
{
for(k = 0; k<10; k++)
{
printf("%6d", array[i]);
i++;
}
printf("\n");

}
printf("Odd and even\n\n\n");
i = 0;
j = 0;
k = 0;

if (array[i] == (array[i]+1)/2 != 0 )
{

for(j = 0; j<100; j++)
{
for(k = 0; k<10; k++)
{
printf("%6d", array[i]);
i++;
}
printf("\n");
}

printf("&6d", array[i]);

}
else
{
for(j = 0; j<100; j++)
{
for(k = 0; k<10; k++)
{
printf("%6d", array[i]);

system("pause");
}
printf("\n");
}

}

system("pause");
}```

2. For an odd even test you can use:

Code:
``` if ((array[i]%2) != 0 )
```

or

Code:
``` if ((array[i]&1) != 0 )
```

3. Thanks I'll update that

4. Trying to figure out what I have done wrong but the second part were it is supposed to determine if it is a odd or even just prints the number 41

5. Originally Posted by phillyflyers
Trying to figure out what I have done wrong but the second part were it is supposed to determine if it is a odd or even just prints the number 41
That's because you set i to zero, but never loop on i.

6. Ok I've worked it a little further Malcolm should I change the odd and even counter to 1? I dont think I have the code right. I'm tyring to put the odd and even values in separate arrays. Thanks guys
Code:
```#include <stdio.h>
#include <stdlib.h>
int main()
{

int array[1000] = {0};
int i = 0;
int j = 0;
int k = 0;
int oddcounter = 0;
int evencounter = 0;
int oddarray[500];
int evenarray[500];

for(i = 0; i<1000; i++)
{
array[i] = rand();
}
i =0;

for(j = 0; j<100; j++)
{
for(k = 0; k<10; k++)
{
printf("%6d", array[i]);
i++;
}
printf("\n");

}
printf("Odd and even\n\n\n");
system("pause");
i = 0;
j = 0;
k = 0;

for(j = 0; j<100; j++)
{
for(k = 0; k<10; k++)
{
printf("%6d", array[i]);
i++;
}
printf("\n");

if (array[i] %2 == 0 )
{
evenarray[evencounter] = array[i];
evencounter++;
}
else
{
oddarray[oddcounter] = array[i];
oddcounter++;
}
printf("%6d", oddarray[oddcounter]); //just to see if it was doing it right

}

system("pause");
}```

7. Originally Posted by phillyflyers
should I change the odd and even counter to 1?
C arrays are indexed starting with 0, so you already have it correct.

8. Thanks but I must have something wrong it doesnt seem to be passing the odd or even values to the odd/even arrays

9. Originally Posted by phillyflyers
Thanks but I must have something wrong it doesnt seem to be passing the odd or even values to the odd/even arrays
In the loop where you count evens and odds, you're incrementing i in the loop that prints, so your program only tests 1 out of every 10 values in array.

10. Code:
```int oddarray[500];
int evenarray[500];```
You have 1000 random numbers and it's very likely that the split between odd and even numbers won't be exactly 500:500. Hence, there will be a buffer overflow in one array.
Since you are a beginner I suggest both arrays should be able to hold 1000 elements.

On line 57 you are printing an uninitialized value in case of an odd number because "oddcounter" is already incremented.

It's also better to not mix processing with output. So write a loop which puts each number in the correct array. And then write a function which outputs an array of any size and use this function to print the even and the odd array.

Bye, Andreas

Popular pages Recent additions