473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reversing elements in an array

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
3 9463
sicarie
4,677 Expert Mod 4TB
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
dru
29
no algorithim just have to figure out that what to put
Nov 7 '06 #3
sicarie
4,677 Expert Mod 4TB
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

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

Similar topics

4
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart...
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: flipflop | last post by:
Sorry if this is a very faq - I haven't seen the answer here though. The quicksort code below sorts an array from large to small. I simply want it to reverse the sort order: small to large, but...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
1
by: rajkumarbathula | last post by:
Hi Could any one help me out in reversing rows/elements of DataTable or String or DataList by using any simple statement? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.