### Even/Odd issues

Everything works, except there is something wrong with the even/odd calculations...it gives weird numbers. Any ideas??

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include #include #define N 9 /* MAIN */ void revArray(int arr[]) { int temp[N]; int i; for(i = 0; i < N; ++i) { temp[i] = arr[N - 1 -i]; } for(i = 0; i < N; ++i) { arr[i] =temp[i]; } } int *revElement(int arr[]) { static int idx = N; idx = --idx; return arr + idx; } void evenOdd(int odd[]) { int i; int evenTemp[N/2]; int oddTemp[(N-1)/2]; char tempOdd; for(i=0; i<(N-1)/2; i++) { if(odd[i]%2 == 0) /* if even integer*/ evenTemp[i] = odd[i]; /*store even numbers temporary */ else oddTemp[i]= odd[i]; /*store odd numbers temporary */ } for(i=0; i<(N-1)/2; i++) { odd[i]=evenTemp[i]; /*swap even numbers first in the array*/ odd[N-1-i]=oddTemp[i]; /*swap odd numbers last in the array */ } } int main(int argc, char **argv) { int a[N]; int idx; int *p = a; while(p < a + N) *p++ = a + N - p; printf("Original: "); p = a; while(p < a + N) printf("%2d ",*p++); /* Part A: Reverse the array */ revArray(a); printf("\nReversed: "); p = a; while(p < a + N) printf("%2d ",*p++); printf("\n"); /* Part B: Return elements in reverse order */ printf("Original: "); for (idx = 0; idx < N; idx++) { printf("%2d ",*revElement(a)); } printf("\n"); /* Part C: Put even numbers first, odd numbers last in the array. Order of the elements is not important as long as all evens are before first odd */ printf("Even: "); evenOdd(a); p = a; while(p < a + N) printf("%2d ",*p++); printf("\n"); system("pause"); }``````
Last edited on
Topic archived. No new replies allowed.