Like Tree1Likes
  • 1 Post By AndiPersti

Need explanation on coding.

This is a discussion on Need explanation on coding. within the C Programming forums, part of the General Programming Boards category; Code: printf("\nEnter location of deletion: "); scanf("%d", &loc); item = a[loc-1]; for(i=loc-1; i<n; i++) { a[i] = a[i+1]; } n--; ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Need explanation on coding.

    Code:
             printf("\nEnter location of deletion: ");
             scanf("%d", &loc);
              
             item = a[loc-1];
             for(i=loc-1; i<n; i++)
             {
                      a[i] = a[i+1];
         }
             n--;
             printf("\nITEM deleted: %d", item);
    what does a[loc-1] and a[i+1] mean? I still don't get it
    Last edited by Alexie; 01-22-2013 at 03:33 AM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    If you have array of 5 elements for example. First element's index is zero, second element's index is one, third's two and so on. Basically array[n] goes from 0 to n-1. If you want to delete third element you actually need to delete element 3-1. But your program don't erase element, it just overwrites it.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    so that mean -1 is somewhat of delete right? if i want delete second element will be 2-1? how about i+1?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by Alexie View Post
    so that mean -1 is somewhat of delete right? if i want delete second element will be 2-1? how about i+1?
    Try reading this: Arrays in C - Cprogramming.com

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    printf("\nEnter location of deletion: ");
    scanf("%d", &loc);
           
    item = a[loc-1];
    for(i=loc-1; i<n; i++)
    {
        a[i] = a[i+1];
    }
    n--;
    Assuming n is the total number of elements in the array, this code has a buffer overflow.

    The deletion works by storing the value to delete in item and then shifting each element after the one which gets deleted one place to the left (a[i] = a[i + 1]). Thus reducing the number of elements in the array by 1 (n--).
    This also means that the for loop should only increment i up to n - 2. In your example i becomes n - 1 which means that i + 1 is n and a[n] is out of bounds.

    Bye, Andreas
    Tanu likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explanation
    By aromash in forum C Programming
    Replies: 3
    Last Post: 12-04-2010, 03:22 PM
  2. @ explanation pls
    By WDT in forum C# Programming
    Replies: 4
    Last Post: 04-22-2009, 08:23 PM
  3. Need a bit explanation!!
    By cBegginer in forum C Programming
    Replies: 4
    Last Post: 04-17-2005, 01:36 AM
  4. need explanation
    By Ssfccsh in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2004, 12:40 PM
  5. Explanation Please
    By atari400 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2003, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21