Change the order of an array

G

Guest

Can anybody tell me how I can put the arguments of an array in a different
order?
 
F

Frane Roje

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
 
J

John Harrison

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
 
R

Rolf Magnus

John said:
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.
 
C

Claudio Puviani

Rolf Magnus said:
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
 
J

Jack Klein

John said:
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.
 
H

Howard

Jack Klein said:
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
 
O

Old Wolf

Jack Klein said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top