# Value swaping/transposing in an array loop

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-18-2013
phillyflyers
Value swaping/transposing in an array loop
Hi Guys,

I hit a road block, I'm trying to swap the values of an array an example would be swapping a[0] with a[1]. I understand you could easily do with using a temp but I'm really stuck on how to do this using a for loop.

temp = a[0]
a[0] = a[1]
a[1] = temp

but when I tried to do something like this for an array I am completely clueless. This is where I got stuck. Thanks
Code:

```void transpose (int a[], int size) {  int i = 0;  int temp;  printf("Now the array is \n");  for (i = 0; i <= size -1; i++)  {     temp = a[i];    }  printf("\n");```
The only thing I can think of is maybe another loop inside that but I really have no clue how that would work
• 04-18-2013
AAnderson
Hi Phil,

It really depends how you want to swap the values, do you want to permute them randomly? Do you want swap every second value?

On a tangent, you should iterate from i = 0 to i < size. I think the reason for this is that it saves you a computation (if someone else more experienced could speak to this?).

Edit: Sorry I think I misread your intent. Do you want to transpose a 2D array?
• 04-18-2013
phillyflyers
Hi thanks for your reply, I just want to swap the current value with the next one. I only have 0-19 so the first swap would be to make 0 1 and 1 0, I just am clueless on how to start with a loop. I know I need to set i to temp but how would I tell it to swap with the next value if its not finished that loop : ). I'm lost
• 04-18-2013
AAnderson
You shouldn't need a second loop for that. Think about how you can do it by manipulating your index (your i) to access different elements during each loop iteration. Just use your temp to hold one of your values while you replace it with the other one.
• 04-18-2013
phillyflyers
Man I'm blank lol I just dont see how I can do that. If I tell it that Temp = a[i] which is what ever i is for that loop (lets say 0) I just cant seem to find a way I would tell the system to go to the next loop change that to 0 and then put 1 in the 0 . if that makes any sense. I'm still bascially stuck at the same part as the code above.
• 04-18-2013
AAnderson
What happens if instead you tell temp to grab a[i+1], or a[i+10] etc?
• 04-18-2013
phillyflyers
I see what your say I tried it it code but it doesnt work it give me the next value so 0 become 1 1 becomes 2 and then 19 gives me -8873948 because it is over the max value of the array
Code:

```for (i = 0; i <= size -1; i++)  {     temp = a[i+1];   a[i+1] = a[i];   a[i] = temp;   printf("%d ", a[i]);  } ```

• 04-18-2013
AAnderson
You've already identified your problem, so how do you stop your loop iterating over the edge of your array? Also, write out what you think your algorithm will do for a very short array (say 5 long), and see if what you write out is what you would expect, and whether it matches what you're getting.
• 04-18-2013
phillyflyers
Ok just got back from a break and I'm still lost. Its not swapping just add 1 to the number, also I was thinking I might have to use an if statement in the while not to go over 19? I've tried looking at my book and google but the book has an example of how to normally do it but not in an array
• 04-18-2013
phillyflyers
Am I going completely wrong about this, I'm starting to think how is it even possible to tell the loop to assign a[i] the next value but for the next loop assign the previous value? My first two I'm trying to switch is 0 should change to 1 and 1 to 0.
• 04-19-2013
rcgldr
Your current code moves the first element of the array past the end of the array, shifting everything else down, including whatever was past the end of the array into the last entry in the array. If you just want to swap evens and odds:

Code:

```    for(i = 0; i < (size-1); i += 2){         temp      = array[i];         array[i]  = array[i+1];         array[i+1] = temp;     }```
• 04-19-2013
phillyflyers
Thanks rcg I managed to figure out I need to variable to get the values but it would not print out what I wanted until I did what you have up there the i +=2, Im not sure but is that i +=2 because I only want to do the loop 10 times?
Code:

```int j= 0;  int i = 0;  int temp;  printf("Now the array is \n");  for (i = 0; i <= size -1; i = i +2)  {     temp = a[i];   a[i] = a[i+1];   j = temp;     printf("%2d %3d ", a[i],j);  }  printf("\n");```
• 04-19-2013
rcgldr
Quote:

Originally Posted by phillyflyers
Im not sure but is that i +=2 because I only want to do the loop 10 times?

I used i += 2, because it's swapping even and odd elements in array. The first loop swaps array[0] and array[1], the next loop swaps array[2] and array[3], the next array[4] and arrray[5], so i needs to increment by 2 for each loop.

If what you want to do is reverse the order of the array, you could copy the array in reverse order to a temp array, then copy from temp back to the original array. If you want to do this in place, using a single temp variable, you'll need a double loop, similar to a bubble sort.
• 04-19-2013
phillyflyers
Thanks again, I'm on the final part and I just wanted a piece of advice. The array is has values 0-19. I need to move all the values up to the next one. So it should look like 19 0 1 2 3 4 ...18 I started by trying to use two variable but the more I think about it I think this can be done with just one since I am only moving up 1. Is there a way to let the array know if it try to step down from the first value of the array to grab the last value? I got the code down but of course the first value is -1 and then the rest are correct. Thanks
Code:

```void shift  (int a[], int size) {  int j= 0;  int i = 0;  int temp;  printf("Now the array is \n");  for (i = 0; i <= size -1; i++)  {     a[i] = i-1;       printf("%3d", a[i]);  }  printf("\n"); }```
• 04-19-2013
rcgldr
Start at the end and work your way backwards creating a loop that does the equivalent of: temp = array[19]; array[19] = array[18]; ... array[1] = array[0]; array[0] = temp; . The temp part would be done outside the loop.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last