By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Reversing elements in an array

P: 29
Problem:
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.

Given an array a , an int variable n containing the number of elements in a , and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , n , k , and temp .

Anyone know how to do this?
Nov 7 '06 #1
Share this Question
Share on Google+
3 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I would just store the value in a variable, then move the value to be switched in, then copy the temp value into the place you just moved it, for however big the array is.

What do you have for your algorithm?
Nov 7 '06 #2

P: 29
dru
no algorithim just have to figure out that what to put
Nov 7 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
no algorithim just have to figure out that what to put
The thing to do when you are programming is to create an algorithm - a method of how you are going to program. If you follow good programming techniques, you can be quite literally 100x faster than a programmer who doesn't.

I would start by making a very general statement of what you need to do:

Manipulate an array

And go through that and just break it down into smaller and smaller bits until it becomes almost code (what is called pseudocode), and you will be able to code it very easily. So from above, I would say

declare an array
reverse the elements in an array

And then do it again:

declare an array
declare a temporary value
for each item in the array
store it in a temporary value
move another value into that spot
move the temporary value into the other spot

Do you see how it is turning into code? (the declares, and the 'for' - though it's not quite there yet) After you do that one or two more times, you will have what looks like code sitting in front of you, and you'll be able to quickly and efficiently write your program.
Nov 7 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.