Value swaping/transposing in an array loop

This is a discussion on Value swaping/transposing in an array loop within the C Programming forums, part of the General Programming Boards category; Hi Guys, I hit a road block, I'm trying to swap the values of an array an example would be ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    41

    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

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    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?

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    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.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    What happens if instead you tell temp to grab a[i+1], or a[i+10] etc?

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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]);
     }
    





  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    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.
    Last edited by AAnderson; 04-18-2013 at 09:00 PM.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    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;
        }

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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");

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    Quote Originally Posted by phillyflyers View Post
    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.

  14. #14
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    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");
    }

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transposing a matrix made from a 1d array
    By brown in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 02:05 PM
  2. Swaping the variables
    By rits in forum C Programming
    Replies: 7
    Last Post: 03-10-2009, 04:40 PM
  3. swaping characters
    By tikelele in forum C Programming
    Replies: 12
    Last Post: 11-06-2007, 02:37 PM
  4. swaping numbers
    By bj31t in forum C++ Programming
    Replies: 14
    Last Post: 03-30-2004, 07:52 AM
  5. swaping using pointers
    By coloughl in forum C Programming
    Replies: 2
    Last Post: 05-17-2002, 08:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21