Thread: help with logic of array that moves elements over 1 time to the right

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    help with logic of array that moves elements over 1 time to the right

    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 do i move the last number out then shift over by 1 and then put the last number back first? here is my code
    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need

    temp = array[5];

    Then you do your shuffling along

    then
    array[0] = temp;
    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
    Banned
    Join Date
    Aug 2017
    Posts
    861
    1 2 3 4 5 6 ends up 2 3 4 5 6 1
    1 2 3 4 5 6 ens up 6 1 2 3 4 5
    which one? i think that second one. not sure.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by userxbw View Post
    1 2 3 4 5 6 ends up 2 3 4 5 6 1
    1 2 3 4 5 6 ens up 6 1 2 3 4 5
    which one? i think that second one. not sure.
    The title specifies it. The code too.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    well this is what I got
    Code:
    userx@slackwhere:~/bin
    $ ./array_shift
    Enter six numbers.
    
    Enter a number : 1
    Enter a number : 2
    Enter a number : 3
    Enter a number : 4
    Enter a number : 5
    Enter a number : 6
     
    6
    1
    2
    3
    4
    5
    if that is the only criteria then mine should get me a passing grade.
    Code:
    userx@slackwhere:~/bin
    $ ./array_shift
    Enter six numbers.
    
    Enter a number : 8
    Enter a number : 7
    Enter a number : 6
    Enter a number : 5
    Enter a number : 4
    Enter a number : 3
    3
    8
    7
    6
    5
    4
    Last edited by userxbw; 10-20-2017 at 03:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a hard time shifting elements
    By saldar05 in forum C Programming
    Replies: 8
    Last Post: 12-03-2012, 05:43 AM
  2. Add elements into an array in C
    By Marria in forum C Programming
    Replies: 6
    Last Post: 09-21-2012, 04:19 PM
  3. Replies: 2
    Last Post: 12-07-2011, 11:22 PM
  4. Replies: 9
    Last Post: 05-29-2010, 10:32 AM
  5. reverse array logic error
    By rippascal in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 10:47 PM

Tags for this Thread