Thread: Shifting elements in an array in C

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    5

    Shifting elements in an array in C

    Hi,

    Let's say I have an array of 10 elements. I want user to enter 9 numbers so that they fill arrays 0 to 8 (9 numbers).
    Now I shift the arrays +1. so array[0] is now array[1] and so on.
    Now I ask user to enter 10th number (fills array 0).

    how can I do that??

    Here's my code(it doesn't shift arrays and doesn't ask for 10th num)

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int a[10];
        int i;
    
    
        printf("\nEnter 9 numbers:\n");
    
    
    for(i=0;i<9;i++)
    {
        printf("%10d numbers remaining\n\n", 9-i);
        scanf("%d", &a[i]);
    }
    
    
    printf( "%s%13s\n", "Element", "Value" );
    for(i=0;i<10;i++)
    {
        printf("%7d%13d\n",i, a[i]);
    }
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Anyone??

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    The thing about the shifting is to be careful not to overwrite something that will be shifted later. In this case, it means that you want to start from the end, i.e., shift a[8] to a[9], then shift a[7] to a[8], etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    It's not that difficult. Try starting from the end of the array (item[9]), not from its beginning. Take item[8] and put it into item[9], take item[7] and put it into item[8], take item[6] and put it into item[7], and so on (you can use "for" loops).

    EDIT: LaserLight has been faster than me!!! Grrrrrrr...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Tee hee.

    That said, do you really need to shift in the first place? It would be simpler and more efficient to just read into a[1] to a[9], and then read into a[0]. You are not constrained to read into elements in order of array position.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Provided that the shift is needed, couldn't one use memmove() too?

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    thanks for all your replies. got my answer here.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    You should have posted that link earlier instead of wasting my time. Notice that my suggestions in post #2 and #5, aldo_baldo's suggestion in post #3, and the suggestions from the post from daniweb are the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,113
    It was also spammed here as well -> Shifting Elements In An Array In C - C And C++ | Dream.In.Code

    Way to earn yourself some black marks.

    People cotton on pretty quick if you persist in cross-posting, and generally just ignore you from then on - rather than risk wasting time on something that's probably answered somewhere else.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in shifting elements in 2 Dimensional array
    By biswanath in forum C Programming
    Replies: 4
    Last Post: 03-01-2013, 09:53 PM
  2. shifting elements in array
    By gamers18 in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2012, 12:28 PM
  3. Deleting and shifting elements in an array
    By sia927777 in forum C Programming
    Replies: 3
    Last Post: 10-13-2010, 01:34 AM
  4. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  5. shifting array elements
    By dP munky in forum C Programming
    Replies: 5
    Last Post: 04-17-2003, 01:35 PM

Tags for this Thread