Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

array shift to right

Misty LoveMisty Love Member Posts: 4

Im trying to make a program that shifts all my elements of array to the right by one and move the last element
into the first position.how would i move the last number out then shift numbers over then move the last number back into the fist position? here is my code
`
#include <stdio.h>

         int main ()
               {
               int array[6];
           int x;
           int temp;

       printf("Enter six numbers.\n\n");


       // ---------- gets numbers from user -------------------          
         for(x = 0; x < 6; x++) {
              printf ("Enter a number : ", x+1);
             scanf ("%d",&array[x]);
        }

           // ----------------- move the last guy out -------------------



              // ----------------- shift everybody over ---------------------      
               for(x = 6; x > 0; x--)
        {
            array[x]=array[x-1];
          }
         // ------------------  put the last guy back into the first one ------         





           // --------------------  print the array contents -------------------       
             for(x = 0; x < 6; x++)
           {

                printf("%d\n", array[x]);

            }


              return 0;
        }```

Comments

  • B00B00 Member Posts: 107 ✭✭✭

    Store last element in temp variable.

  • Tyson BlairTyson Blair Member Posts: 35

    [code]

    include <stdio.h>

    include <stdlib.h>

    void array_shift_right(int *array,int *shifted_array, int arraysize, int shiftplaces);
    int main ()
    {
    int array[6];
    int shifted_array[6];
    int x;
    int temp;
    printf("Enter six numbers.\n\n");
    // ---------- gets numbers from user -------------------
    for(x = 0; x < 6; x++)
    {
    printf ("Enter a number : ", x+1);
    scanf ("%d",&array[x]);
    }
    array_shift_right(array, shifted_array, 6, 1);
    // -------------------- print the array contents -------------------
    for(x = 0; x < 6; x++)
    {
    printf("%d\n", shifted_array[x]);
    }
    return 0;
    }

    void array_shift_right(int *array, int *shifted_array, int arraysize, int shiftplaces)
    {
    int i = 0;
    //int temp[arraysize];
    for(i = 0; i < arraysize; i++)
    {
    if (i == 0)
    {
    shifted_array[i] = array[arraysize-1];
    }
    else
    {
    shifted_array[i] = array[i-1];
    }
    }
    }

    [/code]

  • Tyson BlairTyson Blair Member Posts: 35
    #include <stdio.h>
    #include <stdlib.h>
    void array_shift_right(int *array,int *shifted_array, int arraysize, int shiftplaces);
    int main ()
    {
        int array[6];
        int shifted_array[6];
        int x;
        int temp;
        printf("Enter six numbers.\n\n");
        // ---------- gets numbers from user -------------------
        for(x = 0; x < 6; x++)
            {
             printf ("Enter a number : ", x+1);
            scanf ("%d",&array[x]);
        }
        array_shift_right(array, shifted_array, 6, 1);
        // --------------------  print the array contents -------------------
        for(x = 0; x < 6; x++)
        {
                    printf("%d\n", shifted_array[x]);
        }
        return 0;
    }
    
    void array_shift_right(int *array, int *shifted_array, int arraysize, int shiftplaces)
    {
            int i = 0;
            //int temp[arraysize];
            for(i = 0; i < arraysize; i++)
            {
                    if (i == 0)
                    {
                            shifted_array[i] = array[arraysize-1];
                    }
                    else
                    {
                            shifted_array[i] = array[i-1];
                    }
            }
    }
    
  • Misty LoveMisty Love Member Posts: 4

    can you give me another way without a function? i am trying to understand and i am not that good with functions yet this is what i tried but am not getting the right results
    `
    // ----------------- move the last guy out -------------------

        temp=array[6];
    

    // ----------------- shift everybody over ---------------------
    for(x = 6; x > 0; x--)
    {
    array[x]=array[x-1];
    }

    // ------------------ put the last guy back into the first one ------

         temp=array[6];
    
         array[6]=array[0];
    
         array[6]=temp; 
    

    // -------------------- print the array contents -------------------
    for(x = 0; x < 6; x++)
    {

         printf("%d\n", array[x]);
    
       }
    
    
         return 0;
    

    }`

  • Tyson BlairTyson Blair Member Posts: 35

    hum, I will add comments to the function

    void array_shift_right(int *array, int *shifted_array, int arraysize, int shiftplaces)
    {
        int offset = 0;
        // Loop through the array;
        for(int x = 0; x < arraysize; x++)
        {
            // we check to see if x < then the number of places we want to shift right
            if (x < shiftplaces)
            {
                // here we calculate the offset of the element in array that we want to copy over to shifted_array[x]
                offset = ((arraysize - shiftplaces) + x);
                // here we copy array[offset] to the shifted_array[x] position in the array
                shifted_array[x] = array[offset];                
            }
            else
            {
                // here we calculate the offset of the element in array that we want to copy over to shifted_array[x]
                offset = (x - shiftplaces);
                // here we copy array[offset] to the shifted_array[x] position in the array
                shifted_array[x] = array[offset];            
            }
        }
    }
    
  • Tyson BlairTyson Blair Member Posts: 35

    you can use the code from the for loop, but you need to set arraysize and shiftplaces, to be the correct values, shifted_array can be replaced with temp.

  • Tyson BlairTyson Blair Member Posts: 35
    for(x = 6; x > 0; x--)
     {
        array[x]=array[x-1];
    }
    

    this code is bad you can not directly modify the array you need to modify a temp array then copy the data back over array when the shifting is all done. Or you can store all the values of array into a temp array, then access the temp array to get the values to assign to array[x]

  • Tyson BlairTyson Blair Member Posts: 35
    include < stdio.h >
    
      int main() {
        int array[6];
        int temp[6];
        int x = 0;
        int arraysize = 6;
        int shiftplaces = 1;
    
        printf("Enter six numbers.\n\n");
        // ---------- gets numbers from user -------------------          
        for (x = 0; x < 6; x++) {
          printf("Enter a number : ", x + 1);
          scanf("%d", & array[x]);
        }
    
        // ----------------- shift everybody over to the correct place ---------------------      
        for(x = 0; x < arraysize; x++)
        {
            // ----------------- we move the data to the right by shiftplace places  if shiftpaces = 3, and array contains 1, 2, 3, 4, 5, 6, then temp would contain, 4, 5, 6, 1, 2, 3
            // we check to see if x < then the number of places we want to shift right
            if (x < shiftplaces)
            {
                // here we calculate the offset of the element in array that we want to copy over to shifted_array[x]
                offset = ((arraysize - shiftplaces) + x);
                // here we copy array[offset] to the shifted_array[x] position in the array
                temp[x] = array[offset];                
            }
            else
            {
                // here we calculate the offset of the element in array that we want to copy over to shifted_array[x]
                offset = (x - shiftplaces);
                // here we copy array[offset] to the shifted_array[x] position in the array
                temp[x] = array[offset];            
            }
        }
        // -- copy temp[x] dataq over array[x] data
        for(x = 0; x < arraysize; x++)
        {
            array[x] = temp[x];     
        }
    
        // --------------------  print the array contents -------------------       
        for (x = 0; x < 6; x++) {
          printf("%d\n", array[x]);
        }
        return 0;
    

    }
    here is a version of your code that should shift correctly, to shift 1 place to right set shiftplaces = 1

  • B00B00 Member Posts: 107 ✭✭✭

    this code is bad you can not directly modify the array

    This is perfectly fine, Misty's a beginner and you just overly complicate the problem.

    @Misty Love here's your missing parts in the original code:

    //store last
    temp = array[5]
    
    //your shift loop here
    
    //put it back in
    array[0] = temp
    
Sign In or Register to comment.