English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2007-08-14 17:09:34 · 4 answers · asked by AbrahamsD 1 in Computers & Internet Programming & Design

it would accept 10 numbers using array and swap them...

for example the sequence is 1234567890
it would become
0987654321

2007-08-15 18:02:57 · update #1

4 answers

with which one to swap the 10 numbers?

2007-08-14 18:49:31 · answer #1 · answered by i_am_the_next_best_one 5 · 0 0

the swap part is the tricky so i'll leave the rest to you.

in order for you to swap the value of two variables without using a temporary variable, first, subtract the value of the 2nd variable from the 1st variable and assign to the 2nd variable, second, subtract again the value of the 2nd variable from the 1st variable and assign to the 1st variable and lastly, add the 2nd variable to the 1st variable and assign it to the 2nd variable

simulation:

num[1] = 21
num[2] = 6

1. subtract the value of the 2nd variable from the 1st variable and assign to the 2nd variable

num[2] = num[1] - num[2]
num[2] = 21 - 6
num[2] = 15

2. subtract again the value of the 2nd variable from the 1st variable and assign to the 1st variable

num[1] = num[1] - num[2]
num[1] = 21 - 15
num[1] = 6

3. add the 2nd variable to the 1st variable and assign it to the 2nd variable

num[2] = num[1] + num[2]
num[2] = 6 + 15
num[2] = 21

giving us:
num[1] = 6
num[2] = 21

so the code for the swap is:

num[2] = num[1] - num[2];
num[1] = num[1] - num[2];
num[2] = num[1] + num[2];

2007-08-15 00:26:30 · answer #2 · answered by siacojin 2 · 0 0

I don't understand what you are looking for. Can you provide more details?

2007-08-14 17:46:54 · answer #3 · answered by anonymous 2 · 0 0

You can use the properties of Xor.

2007-08-14 17:17:02 · answer #4 · answered by mdigitale 7 · 0 0

fedest.com, questions and answers