| | |
Pointers and Sorting Array by Even and Odd numbers
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 13
Reputation:
Solved Threads: 0
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:
My Code:
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #define N 9 /* MAIN */ int main(int argc, char **argv) { /* Load the array with numbers */ 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"); exit(0); }
My Code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #define N 9 void revArray(int *p); int *revElement(int *p); void evenOdd(int *p); /* MAIN */ int main(int argc, char **argv) { /* Load the array with numbers */ 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"); exit(0); } void revArray(int *p) { int arr[N], i; int *s = p; for(i = N-1; i >=0; --i) { arr[i] = *p++; } for(i = 0; i < N; ++i) { *(s+i) = arr[i]; } return; } int *revElement(int *p) { static int idx2 = N; idx2 = --idx2; return p + idx2; } /*Sorting Array putting even numbers first, then odd. I can't figure out how to do this???*/ void evenOdd(int *p) { int b[N],j=0,c[N],k=0; int arr[N], i; for(i = 0; i < N; ++i) { if (arr[N] %2==0) { b[j]=arr[i]; j++; } else if (arr[N] %2 !=0) { c[k]=arr[i]; k++; } } }
Last edited by taikoprogrammer; Aug 4th, 2009 at 7:39 pm.
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
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- sort even and odd numbers from an array (C++)
- Sorting an Array of numbers in VB 2008 (Visual Basic 4 / 5 / 6)
- sorting 2d array (C++)
- sort even and odd numbers for an array (C)
- SORTING ARRAY (C++)
- C++ How to allow only odd numbers to be inputted (C++)
- Sorting a 2d array of strings in C++?? (C++)
- print odd and even numbers (Assembly)
- sorting an array of string (C)
Other Threads in the C Forum
- Previous Thread: TurboC3 not compiling basic programs help....
- Next Thread: Salary problem...(Beginner)
Views: 430 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h





