Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Reversing the elements of an array please help?

    Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

    This is what i got but its not working. It compiles but wont change the contents of the array

    using namespace std;
    int main()
    {
    int score[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

    for(int k = 0; k < 10; k++)
    {
    int temp = score[k]; // store the first element value in temp
    score[k] = score[9-k]; // store the last element value in the first element
    score[9-k] = temp; // store temp in the last element
    }

    return 0;
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Looks to me that it will. You just need to stop halfway through.
    You should ditch the magic numbers and base it off of .length instead.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •