Pointers and Sorting Array by Even and Odd numbers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 13
Reputation: taikoprogrammer is an unknown quantity at this point 
Solved Threads: 0
taikoprogrammer taikoprogrammer is offline Offline
Newbie Poster

Pointers and Sorting Array by Even and Odd numbers

 
0
  #1
Aug 4th, 2009
Hi Everyone,

I need some help. So I am given this code and I have to make the functions for revArray, *reveElement, and evenOdd. So far I have figured out how to reverse the Array and print it. But I'm having some trouble sorting the array. I'm supposed to put the even numbers first then the odd numbers last in the array. But i can't figure out how... Any tips, hints, and help would be much appreciated. Thank you!

Given:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 9
  4.  
  5.  
  6. /* MAIN */
  7. int main(int argc, char **argv)
  8. {
  9.  
  10. /* Load the array with numbers */
  11. int a[N];
  12. int idx;
  13. int *p = a;
  14. while(p < a + N) *p++ = a + N - p;
  15. printf("Original: ");
  16. p = a; while(p < a + N) printf("%2d ",*p++);
  17.  
  18. /* Part A: Reverse the array */
  19. revArray(a);
  20. printf("\nReversed: ");
  21. p = a; while(p < a + N) printf("%2d ",*p++);
  22. printf("\n");
  23.  
  24. /* Part B: Return elements in reverse order */
  25. printf("Original: ");
  26. for (idx = 0; idx < N; idx++) {
  27. printf("%2d ",*revElement(a));
  28. }
  29. printf("\n");
  30.  
  31. /* Part C: Put even numbers first, odd numbers last in the array. Order of
  32.   the elements is not important as long as all evens are before first odd */
  33. printf("Even: ");
  34. evenOdd(a);
  35. p = a; while(p < a + N) printf("%2d ",*p++);
  36. printf("\n");
  37.  
  38.  
  39. system("pause");
  40. exit(0);
  41. }

My Code:

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define N 9
  5.  
  6. void revArray(int *p);
  7. int *revElement(int *p);
  8. void evenOdd(int *p);
  9.  
  10.  
  11. /* MAIN */
  12. int main(int argc, char **argv)
  13. {
  14.  
  15. /* Load the array with numbers */
  16. int a[N];
  17. int idx;
  18. int *p = a;
  19. while(p < a + N) *p++ = a + N - p;
  20. printf("Original: ");
  21. p = a; while(p < a + N) printf("%2d ",*p++);
  22.  
  23. /* Part A: Reverse the array */
  24. revArray(a);
  25. printf("\nReversed: ");
  26. p = a; while(p < a + N) printf("%2d ",*p++);
  27. printf("\n");
  28.  
  29. /* Part B: Return elements in reverse order */
  30. printf("Original: ");
  31. for (idx = 0; idx < N; idx++) {
  32. printf("%2d ",*revElement(a));
  33. }
  34. printf("\n");
  35.  
  36. /* Part C: Put even numbers first, odd numbers last in the array. Order of
  37.   the elements is not important as long as all evens are before first odd */
  38. printf("Even: ");
  39. evenOdd(a);
  40. p = a; while(p < a + N) printf("%2d ",*p++);
  41. printf("\n");
  42.  
  43.  
  44. system("pause");
  45. exit(0);
  46. }
  47.  
  48.  
  49. void revArray(int *p)
  50. {
  51.  
  52. int arr[N], i;
  53.  
  54.  
  55. int *s = p;
  56.  
  57. for(i = N-1; i >=0; --i)
  58.  
  59. {
  60.  
  61. arr[i] = *p++;
  62.  
  63. }
  64.  
  65. for(i = 0; i < N; ++i)
  66.  
  67. {
  68.  
  69. *(s+i) = arr[i];
  70. }
  71. return;
  72.  
  73.  
  74. }
  75.  
  76. int *revElement(int *p)
  77. {
  78.  
  79. static int idx2 = N;
  80.  
  81. idx2 = --idx2;
  82.  
  83. return p + idx2;
  84.  
  85. }
  86.  
  87. /*Sorting Array putting even numbers first, then odd. I can't figure out how to do this???*/
  88. void evenOdd(int *p)
  89.  
  90. {
  91. int b[N],j=0,c[N],k=0;
  92. int arr[N], i;
  93.  
  94. for(i = 0; i < N; ++i)
  95.  
  96. {
  97.  
  98. if (arr[N] %2==0)
  99.  
  100. {
  101. b[j]=arr[i];
  102. j++;
  103. }
  104. else if (arr[N] %2 !=0)
  105. {
  106.  
  107. c[k]=arr[i];
  108. k++;
  109. }
  110. }
  111. }
Last edited by taikoprogrammer; Aug 4th, 2009 at 7:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 17
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Pointers and Sorting Array by Even and Odd numbers

 
0
  #2
Aug 4th, 2009
Pass through the input array twice. The first time, if the number is even, put it in the output array. The second time, if the number is odd, put it in the output array.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 13
Reputation: taikoprogrammer is an unknown quantity at this point 
Solved Threads: 0
taikoprogrammer taikoprogrammer is offline Offline
Newbie Poster

Re: Pointers and Sorting Array by Even and Odd numbers

 
0
  #3
Aug 4th, 2009
Sorry I don't really understand. How would the computer know which numbers are even and odd when going through the array?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: Pointers and Sorting Array by Even and Odd numbers

 
0
  #4
Aug 5th, 2009
Originally Posted by taikoprogrammer View Post
Sorry I don't really understand. How would the computer know which numbers are even and odd when going through the array?
Research and learn about the modulo operator (%).
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 430 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC