Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tdabboud

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

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. Tomas.A
    Best Response
    You've already chosen the best response.
    Medals 4

    does order matters?

    • 2 years ago
  2. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    no order does not matter

    • 2 years ago
  3. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    void rearrange(int a[]) {int i, b[SIZE]; for(i=0;i<SIZE;i++) {if(a[i]%2) {THEN ITS ODD}

    • 2 years ago
  4. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    thats what i have so far

    • 2 years ago
  5. Tomas.A
    Best Response
    You've already chosen the best response.
    Medals 4

    you can create new array and check old array if it's even and put those numbers there :D and later put odd numbers

    • 2 years ago
  6. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    I 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?

    • 2 years ago
  7. Tomas.A
    Best Response
    You've already chosen the best response.
    Medals 4

    no you need to index from 0, not to same position

    • 2 years ago
  8. Tomas.A
    Best Response
    You've already chosen the best response.
    Medals 4

    and then increment when you add number http://pastebin.com/F8HT8rmT

    • 2 years ago
  9. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    oh!!! okay that makes sense now. thanks for the help thomas, i appreciate it

    • 2 years ago
  10. Tomas.A
    Best Response
    You've already chosen the best response.
    Medals 4

    i think it's bad solution but it works :D

    • 2 years ago
  11. tdabboud
    Best Response
    You've already chosen the best response.
    Medals 0

    ha I am sure there are other ways to do it but hey it works for me!

    • 2 years ago
  12. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 0

    You could sort the numbers by their modulus value: qsort( number % 2 ); Something like that.

    • 2 years ago
  13. adhokshaj
    Best Response
    You've already chosen the best response.
    Medals 0

    Actually 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.

    • 2 years ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.