A community for students.
Here's the question you clicked on:
 0 viewing
tdabboud
 4 years ago
how can i rearrange an array so all the even numbers will have indices less than those for the odd numbers.
This is in C.
I have code for the rest of the program
tdabboud
 4 years ago
how can i rearrange an array so all the even numbers will have indices less than those for the odd numbers. This is in C. I have code for the rest of the program

This Question is Closed

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0no order does not matter

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0void rearrange(int a[]) {int i, b[SIZE]; for(i=0;i<SIZE;i++) {if(a[i]%2) {THEN ITS ODD}

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0thats what i have so far

Tomas.A
 4 years ago
Best ResponseYou've already chosen the best response.4you can create new array and check old array if it's even and put those numbers there :D and later put odd numbers

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0I am not sure if i know how to do that but i can try. if(a[i]%2) {b[i] = a[i];} //store odd values into new array else{} //I am not sure what to do with it if its even then. make another array?

Tomas.A
 4 years ago
Best ResponseYou've already chosen the best response.4no you need to index from 0, not to same position

Tomas.A
 4 years ago
Best ResponseYou've already chosen the best response.4and then increment when you add number http://pastebin.com/F8HT8rmT

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0oh!!! okay that makes sense now. thanks for the help thomas, i appreciate it

Tomas.A
 4 years ago
Best ResponseYou've already chosen the best response.4i think it's bad solution but it works :D

tdabboud
 4 years ago
Best ResponseYou've already chosen the best response.0ha I am sure there are other ways to do it but hey it works for me!

rsmith6559
 4 years ago
Best ResponseYou've already chosen the best response.0You could sort the numbers by their modulus value: qsort( number % 2 ); Something like that.

adhokshaj
 4 years ago
Best ResponseYou've already chosen the best response.0Actually this problem is very easy. Declare another integer array with same length, and then copy even numbers first, then append remaining numbers (thee odd ones) in last of the second array. The code snippet to do this is shown below: int[] arrange(int a[], size) { int *b = new int[size]; int index = 0; // variable to track position in second array // copy even numbers first for (int i = 0; i < size; ++i) { if (a[i]%2 == 0) { b[index] = a[i]; ++b; } } // copy all odd numbers for (int i = 0; i < size; ++i) { if (a[i]%2 != 0) { b[index] = a[i]; ++b; } } // now return new array return b; } Hope this will help you. Happy programming.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.