473,387 Members | 1,517 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,387 software developers and data experts.

Change the order of an array

Can anybody tell me how I can put the arguments of an array in a different
order?
Jul 22 '05 #1
7 6959
Well it depends,
you can swap to elements easily, but I don't think that
is what you are looking for. Maybe if you want to let's say
sort the array(of ints) in ascending order you could use
qsort() which would rearange your array.

HTH

--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
<h@h.h> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
Can anybody tell me how I can put the arguments of an array in a different
order?

Jul 22 '05 #2

<h@h.h> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
Can anybody tell me how I can put the arguments of an array in a different
order?


This code puts the elements (not arguments) of an array in a different
order, specifically it swaps the first two elements.

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int t = a[0];
a[0] = a[1];
a[1] = t;

Does that help?

john
Jul 22 '05 #3
John Harrison wrote:

<h@h.h> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
Can anybody tell me how I can put the arguments of an array in a
different order?


This code puts the elements (not arguments) of an array in a different
order, specifically it swaps the first two elements.

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int t = a[0];
a[0] = a[1];
a[1] = t;

Does that help?


For int, you could of course also use the nifty xor trick:

a[0] ^= a[1];
a[0] ^= a[1];

And using the C++ standard library, you could use:

std::swap(a[0], a[1]);

which is to prefer because it is the solution that is best at showing
the intent.

Jul 22 '05 #4
"Rolf Magnus" <ra******@t-online.de> wrote
For int, you could of course also use the nifty xor trick:

a[0] ^= a[1];
a[0] ^= a[1];

You're missing one operation. The correct sequence should be:

a[0] ^= a[1];
a[1] ^= a[0];
a[0] ^= a[1];

Claudio Puviani
Jul 22 '05 #5
On Sun, 02 May 2004 14:46:38 +0200, Rolf Magnus <ra******@t-online.de>
wrote in comp.lang.c++:
John Harrison wrote:

<h@h.h> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
Can anybody tell me how I can put the arguments of an array in a
different order?

This code puts the elements (not arguments) of an array in a different
order, specifically it swaps the first two elements.

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int t = a[0];
a[0] = a[1];
a[1] = t;

Does that help?


For int, you could of course also use the nifty xor trick:

a[0] ^= a[1];
a[0] ^= a[1];


Except that for signed ints you run the risk of creating a trap value
and causing undefined behavior.
And using the C++ standard library, you could use:

std::swap(a[0], a[1]);

which is to prefer because it is the solution that is best at showing
the intent.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6

"Jack Klein" <ja*******@spamcop.net> wrote in message
For int, you could of course also use the nifty xor trick:

a[0] ^= a[1];
a[0] ^= a[1];


Except that for signed ints you run the risk of creating a trap value
and causing undefined behavior.


A trap value? Aren't all integer values legal? I thought, by the standard,
a signed int covered all bit values, from -maxint to maxint-1. If that's
correct, then no amount of bit twiddling can produce an illegal value.

-Howard

Jul 22 '05 #7
Jack Klein <ja*******@spamcop.net> wrote:
Rolf Magnus <ra******@t-online.de> wrote:

For int, you could of course also use the nifty xor trick:

a[0] ^= a[1];
a[0] ^= a[1];


Except that for signed ints you run the risk of creating a trap value
and causing undefined behavior.


C&V please? I don't doubt you're right (for example, I can see how
one might create -0 using xor), but all I can see in the standard is
"arithmetic operations may not generate trap representations,
except for the case of overflow", and the term "arithmetic operations"
is not defined. At least one bitwise operator (~) is called an arithmetic
operator, and the operands of ^ undergo "arithmetic conversions".
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
1
by: Sue Adams | last post by:
I have a form on an asp page and am trying to write the code to place the values of all checkboxes that have been selected, into an array. The checkbox values will be used in a dynamic sql statement...
9
by: Steven T. Hatton | last post by:
The following works: template <typename T> struct ID3M{ static const T ID; }; template <typename T> const T ID3M<T>::ID = {{1,0,0},{0,1,0},{0,0,1}};
0
by: William Stacey [MVP] | last post by:
This code worked on fx 1.1 and now I get a "Keyset does not exist" error when trying to SignData with RSA under FX2.0. Smells like some security error, but can't debug it as I think error is...
4
by: Charles | last post by:
Hello Everyone, I have been gettting great feedback from microsoft.public.vc.language group but after doing more searching I think my post should be directed to this group. I am trying to make...
5
by: kalki70 | last post by:
Hello, I've been loooking for info about this issue, but I still can't find. If I create an array of objects, are the constructors called in some predefined order, or it is compiler-dependent? ...
4
by: kevincw01 | last post by:
Anyone have a clever way to retrieve for example, items 0-29 from an array of size N>29, in random order? The catch is that I dont want to print any items more than once and I dont want to miss...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
1
by: aRTx | last post by:
<? /* Directory Listing Script - Version 2 ==================================== Script Author: Artani <artan_p@msn.com>. www.artxcenter.com REQUIREMENTS ============ This script requires...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.