Reverse array by swapping

This is a discussion on Reverse array by swapping within the C Programming forums, part of the General Programming Boards category; Hi, In Below code Code: #include <stdio.h> int main() { int array[100], n, c, t, end; scanf("%d", &n);//4 number iam ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Reverse array by swapping

    Hi,

    In Below code

    Code:
    #include <stdio.h>
    int main()
     {
    
      int array[100], n, c, t, end;
     
      scanf("%d", &n);//4 number iam giving as input
    
      end = n - 1;//end=3
     
      for (c = 0; c < n; c++) {
    
        scanf("%d", &array[c]);//arr[0]=2,arr[1]=3,arr[2]=4,arr[3]=5
      }
     
      for (c = 0; c < n/2; c++)
        {
    
        t          = array[c];
        array[c]   = array[end];
        array[end] = t;
    
    //so for loop 
    
    /*for(c=0;c<2;c++),it would be 
    
    t=arr[0]         ==it would be t=2 as arr[0]=2
    arr[0]=arr[3]    ==it would be arr[0]=5 
    arr[3]=t         ==it would be arr[3]=2
    
    so now arr[0]=5
           arr[3]=2
                
    
      
    /*Now for second for loop
    
    /*for(c=1;c<2;c++) it would be 
    
    t=arr[1]          ==it would be t=3
    arr[1]=arr[3]//iam stuck here ,whatz this arr[end] depicts whether it is arr[3] 
    value or arr[2] value */   
    
      
      }
     
      printf("Reversed array elements are:\n");
     
      for (c = 0; c < n; c++) {
        printf("%d\n", array[c]);
      }
     getch();
      return 0;
    }

    i got stuck in second for loop as in comment

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take four coins of different sizes, and set them up in a line.

    Now make the swaps needed to reverse their order. Repeat that (typically, several times may be needed), until you see the pattern of logical steps you are making, so naturally you don't even think about it anymore.

    Write down those steps you see. That's the backbone of your reverse program's logic.

    I have no idea what you need a second for loop to do.

    Insert a print statement inside the loop, and have it print out the index numbers you're working with for the swaps. By adding a getchar() after that (I do it on the same line for convenience), you get a nice pause to see what's going on in your program.

    I'm not helping you, in this case, by helping you with the code. You can figure this out, and learn how to design and debug a simple program, all at the same time here. Work through it, and see if it doesn't make the "light" bulb go on brighter.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    Hey,

    Thanks,will work out the same.

    Coin example will help i guess....I haven't thought of it!!!!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are a bucketful of tricks in programming that use specifics of data structures or binary schemes (bits especially), to make a whiz-bang solution to a problem, but they're more advanced, and generally not part of the general problem solving assignments that you see in lower programming classes.

    "Dancing Links" is one example. It uses linked lists so smartly to solve "coloring" problems and more - it's a beautiful thing, but you'll never see it in a beginning or intermediate programming course. Just too advanced, generally.

    For all the rest of the problems, looking at how we solve the problem is a good start - we're a bit lazy, which leads to a fairly efficient way of doing things.

    The surprising thing is how many times you have to run through the exercises sometimes, before you quit using the part of your brain that "does simple tasks automatically", and can see just what the logical steps are that you are using. I recall one problem that I spent many hours working through, because I just couldn't see the algorithm I wanted - and this one really needed to be *very* efficient. It payed off, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in swapping puzzle contain of the array...
    By tianwu in forum C Programming
    Replies: 4
    Last Post: 11-28-2011, 09:08 AM
  2. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  3. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM
  4. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 02:18 PM
  5. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 02:31 AM

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