Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-16-2012, 08:03 AM   PM User | #1
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
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;
}
sabi is offline   Reply With Quote
Old 10-16-2012, 02:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:25 AM.


Advertisement
Log in to turn off these ads.