Flipping a section of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Acolyte
    New Member
    • Jan 2007
    • 20

    Flipping a section of an array

    Hey, another question. How do you flip a section of a one-dimensional array? For example, you have array[5] = {1,2,3,4,5}, and I want to flip the last 3 numbers, to produce array[5] = {1,2,5,4,3}.

    Any help would be much appreciated!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Acolyte
    Hey, another question. How do you flip a section of a one-dimensional array? For example, you have array[5] = {1,2,3,4,5}, and I want to flip the last 3 numbers, to produce array[5] = {1,2,5,4,3}.

    Any help would be much appreciated!
    Just write a function that does it. The function should take the array, the size of the array, the start position and the end position for the swap. Let's see what you think it should be like first.

    Comment

    • Acolyte
      New Member
      • Jan 2007
      • 20

      #3
      Alright, here's what I've got so far for it:
      Code:
      #include <stdio.h>
      void main (void)
      
      {
              int array[5], temparray[5], f, l, flip;
              printf("Please enter the numbers: ");
              for (f = 0; f < 5; f ++)
      
              {
                      scanf("%d", &array[f]);
                      temparray[f] = array[f];
              }
      
              printf("Please enter the number to start the flip from: ");
              scanf("%d", &flip);
      
              l = 5;
      
              for (f = flip; f < 6; f ++)
              {
                      array[f] = temparray[l];
                      array[l] = temparray[f];
                      l--;
              }
      
              for (f = 0; f < 5; f ++)
              {
                      printf("%d ", array[f]);
              }
      
      }

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Code:
        for (f = flip; f < 6; f ++)
        {
           array[f] = temparray[l];
           array[l] = temparray[f];
           l--;
        }
        Two problems here:

        1) Your loop goes from flip to 5. This will result in accessing the 6th member of each array (which hasn't been defined). You should instead write for (f = flip; f < 5; f++) so that you will never try to access array[5] (6th element).

        2) This loop, as it is, will flip the numbers - twice. Your array will end up the same as before. This is because you are visiting each element to be flipped and flipping it - but in each execution, two numbers are being flipped, not just one.

        Comment

        • Manjiri
          New Member
          • Nov 2006
          • 40

          #5
          Originally posted by Acolyte
          Alright, here's what I've got so far for it:
          Code:
          #include <stdio.h>
          void main (void)
          
          {
                  int array[5], temparray[5], f, l, flip;
                  printf("Please enter the numbers: ");
                  for (f = 0; f < 5; f ++)
          
                  {
                          scanf("%d", &array[f]);
                          temparray[f] = array[f];
                  }
          
                  printf("Please enter the number to start the flip from: ");
                  scanf("%d", &flip);
          
                  l = 5;
          
                  for (f = flip; f < 6; f ++)
                  {
                          array[f] = temparray[l];
                          array[l] = temparray[f];
                          l--;
                  }
          
                  for (f = 0; f < 5; f ++)
                  {
                          printf("%d ", array[f]);
                  }
          
          }

          Hello Friend... This logic will help you to get the result...
          So try it out... I have done little modification in your code.. And whole logic resides in while loop....

          Code:
          #include <stdio.h>
          int  main (void)
          
          {
                  int array[5],i,flip,n,f,l,temp;
                  printf("How many numbers\n");
                  scanf("%d",&n);
                  printf("Please enter the numbers: ");
                  for(i=0; i<n; i++)
                  scanf("%d",&array[i]);
                  printf("Please enter the number to start the flip from: ");
                  scanf("%d", &flip);
          
          
                  f=flip;
                  l=n-1;
                  while(f<l)
               {
          
                  temp=array[f];
                  array[f]=array[l];
                  array[l]=temp;
                  printf("%d\n%d\n",array[f],array[l]);
                  f++;
                  l--;
                }
                  printf("After flipping the array is\n");
                  for(i=0; i<n; i++)
                  printf("%d",array[i]);
          
          return 0;

          Comment

          • Acolyte
            New Member
            • Jan 2007
            • 20

            #6
            Alright, I've made the modifications, and got parts of the array to flip. Now, the next part.
            By the way, I'm supposed to be working towards creating a Pancake Sorting program, so if anybody has any help at all for that, I'd love to see it.

            Anyways, here's my next hurdle-I'm trying to get it to find the largest number, have it switch places with the first number, then invert the whole array. As usual, here's what I have:

            Code:
            #include <stdio.h>
            void main (void)
            
            {
                    int array[5], temparray[5], f, g, l, flip, lgp, lgn = 0;
                    printf("Please enter the numbers: ");
                    for (f = 0; f < 5; f ++)
            
                    {
                            scanf("%d", &array[f]);
                            temparray[f] = array[f];
                    }
            
                    l = 4;
            
                    for (f = 0; f < 6; f ++)
                    {
                            if (array[f] > array[f + 1])
                                    lgp = f;
                                    lgn = array[f];
                    }
            
                    array[0] = lgn;
                    array[lgp] = temparray[0];
            
            
                    for (f = 0; f < lgn; f ++)
                    {
                            array[f] = temparray[l];
                            l--;
                    }
            
                    for (f = 0; f < 5; f ++)
                    {
                            printf("%d ", array[f]);
                    }
            
            }

            Comment

            Working...