Deleting an array element in C

后端 未结 7 1799
自闭症患者
自闭症患者 2021-01-06 14:51

I wrote the following program to delete an array element entered by the user.

#include 
#include 

void main() {
    int j, i,          


        
相关标签:
7条回答
  • 2021-01-06 15:03
    #include<stdio.h>
    
    
    int main(){
    int size;
    int array[20];
    int delete_pos;
    int i;
    
    printf("Enter the Size of the Array :");
    scanf("%d",&size);
    
    
    for(i=0;i<=size-1;i++){                                //no of elements taken are 1 less than size of the array asked.
        printf("\nEnter the element[%d] :",i+1);
        scanf("%d",&array[i]);
    }
    
    printf("\nEnter the Position of the array to be deleted :");
    scanf("%d",&delete_pos);
    
    
    for(i=delete_pos-1;i<=size;i++){                    //every element of the array is replaced by array on next position.
        array[i]=array[i+1];}
    
    size=size-1;                                       // Reducing the size of the array as one element is deleted.
    printf("Your new array is \n");
    for(i=0;i<=size-1;i++){                            //printing the entire new array.
        printf("%d ",array[i]);
    
    }
    printf("\n\n");
    return 0;
    }
    
    0 讨论(0)
  • 2021-01-06 15:06

    Your method with 2 nested for loops is too complicated. You can simple scan the array with an index i and copy all elements different from key with a different index len. The resulting array length is the final value of len.

    Here is a modified version:

    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
        int a[100];
        int i, n, key, len;
    
        clrscr();
        printf("Enter the number of elements: ");
        if (scanf("%d", &n) != 1) {
            printf("invalid input\n");
            return 1;
        }
        if (n < 0 || n > 100) {
            printf("invalid number of elements\n");
            return 1;
        }
        printf("\nEnter the elements:\n");
        for (i = 0; i < n; i++) {
            if (scanf("%d", &a[i]) != 1) {
                printf("invalid input\n");
                return 1;
            }
        }
        printf("\nEnter the element to delete: ");
        if (scanf("%d", &key) != 1) {
            printf("invalid input\n");
            return 1;
        }
    
        for (i = len = 0; i < n; i++) {
            if (a[i] != key)
               a[len++] = a[i];
        }
    
        printf("\nThe new array is:\n");
        for (i = 0; i < len; i++)
            printf("%d ", a[i]);
        printf("\n");
        getch();
        return 0;
    }
    

    Notes:

    • The prototype for main without arguments is int main(void) and it is considered good style to return 0 for success.

    • always test the return value of scanf(). This prevents many bugs and undefined behavior for invalid input. It also saves a lot of time looking in the wrong places when input was just invalid.

    • avoid naming a variable l as it looks too close to 1 in many fixed pitch fonts.

    • always terminate the program output with a newline.

    0 讨论(0)
  • 2021-01-06 15:07

    use a new array.

    int array[l];
    int k=0;
    for(i=0;i<l;i++)
    {
     if(a[i]!=key)
     {
      array[k]=a[i];
      k++;
     }
    
    }
    
    0 讨论(0)
  • 2021-01-06 15:10

    Change "if" to "while":

    
        for(i=0;i<l;i++)
        {
            while (i<l && a[i]==key)
            {
                for(j=i;j<l;j++)
                    a[j]=a[j+1];
                l--;    //Decreasing the length of the array
            }
        }
    
    
    0 讨论(0)
  • 2021-01-06 15:16

    After l-- put i-- too as shown below

    if(a[i]==key)
      {
       for(j=i;j<l;j++)
        a[j]=a[j+1];
       l--;    //Decreasing the length of the array
       i--;    //check again from same index i
      }
    
    0 讨论(0)
  • 2021-01-06 15:16

    Other posters have given you 2 solutions ... I think understanding why it happens is good too :)

    Let's take your example 1, 2, 2, 3, 5 and follow the code line by line

    i = 0;             /* first time through the loop; i "points" to 1 */
    if (a[i] == 2) ... /* nope; next loop */
    i = 1;
    if (a[1] == 2) ... /* yes! let's go inside the if */
                       /* move all elements back
                       ** and "decrease" array length */
                       /* array is now 1, 2, 3, 5 */
                       /* next loop */
    i = 2;
    if (a[i] == 2) ... /* nope; OH! Wait ...
                       ** a[1] is the new 2 and it wasn't checked */
    
    0 讨论(0)
提交回复
热议问题