Thread: Shift array elements

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    3

    Shift array elements

    Hello guys, I'm a newbie, and I've started to learn C language about 3 months ago. It's going ok, I'm not super content with my progress, but ok. Anyways, I'm learning from a book Let us C by Yashavant Kanetkar, and I came across a problem where I need a better solution.

    ***Given an array p[5], write a function to shift it circularly left by two positions. Thus, if p[0] = 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61 then after the shift p[0] = 28, p[1] = 19, p[2] = 61, p[3] = 15 and p[4] = 30. Call this function for a (4 x 5 ) matrix and get its rows left shifted.***


    My code looks like this. Obviously I haven't done the whole task, only shifted the elements of an array (in the main function). It does its job, but I don't like the code. Nothing comes on my mind when it comes to shifting the first and the second element to an array. Any comment would be helpful! Thank you in advance.

    Capture — ImgBB

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    Code should be copy/pasted between [code][/code] tags.
    Fuzzy pictures on temporary external sites don't allow us to test your code, or for people in the future to even see your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    Sorry, here is the code

    Code:
     
    #include <stdio.h>
    int main ()
    {
        int p[5]={15,30,28,19,61};
        int *a;
        a=p;
        
        int i,fir,sec,temp;
        
        printf ("Before shift: ");
        for (i=0;i<5;i++)
        {
            printf ("%d ",*(a+i));
        }
        
        printf ("\n");
        printf ("After shift:");
        fir=a[0];
        sec=a[1];
        for (i=0;i<3;i++)
        {
            temp=a[i];
            a[i]=a[i+2];
            a[i+2]=temp;
        }
        
        *(a+3)=fir;
        *(a+4)=sec;
        
        for (i=0;i<5;i++)
        printf ("%d ",a[i]);
        
        
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    294
    Maybe you might want to add a couple of functions, making you 'main()' function look something like this:

    Code:
    int main ()
    {
        int p[5]={15,30,28,19,61};
    
    
        printArray("Before shift", p, 5);
    
        shiftByTwo(p,5);
    
        printArray("After shift", p, 5);
    
    
        return 0;
    }
    Sadly there really isn't a much better way to do the shifting...

  5. #5
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    Thanks for the reply. I've added a function, and now the code looks decent, at least.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    888
    Code:
    static void arrayShiftLeft( int *arr, size_t nelems )
    {
      if ( nelems )
      {
        int tmp = *arr;
        memmove( arr, arr+1, (nelems - 1)*sizeof *arr );
        arr[nelems - 1] = tmp;
      }
    }
    
    void arrayShiftElementsLeft( int *arr, size_t nelems, unsigned int n )
    {
      if ( nelems ) // to avoid unecessary calls
        while ( n-- )
          arrayShiftLeft( arr, nelems );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2018, 07:31 PM
  2. Shift right for an array .
    By amir1986 in forum C Programming
    Replies: 4
    Last Post: 01-22-2011, 06:56 AM
  3. How to shift elements of an array?
    By zeebo17 in forum C Programming
    Replies: 25
    Last Post: 06-09-2010, 07:44 PM
  4. Left Shift elements of a 2D array
    By w2look in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:13 AM
  5. how to make array elements shift one to the right?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2002, 11:28 PM

Tags for this Thread