0

I was trying to delete an element from an array, I have succeeded, but I got unknown numbers (0 1804185664) after deleting an element from array.

Can someone help me out why did that appear?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[30];
    int n,i,pos;
    
    printf("how many ele\n");
    scanf("%d", &n);
    printf("enter %d ele\n",n);
    for(i=0;i<n;i++)
    {
      scanf("%d", &a[i]);
    }
    
    printf("enter position of element to be deleted\n");
    scanf("%d", &pos);
    for(i=pos-1; i<=n;i++)
    {
      a[i]= a[i+1];
    }
    printf("elements in array are\n");
    for(i=0; i<=n;i++)
    {
      printf("%d\t", a[i]);
    }
    return 0;
}

Output:

how many ele
7
enter 7 ele
1
5
34
6
23
78
5
enter position of element to be deleted
3
elements in array are
1   5   6   23  78  5   0   1804185664  
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    You are getting garbage values from memory locations beyond the array because of this line `a[i] = a[i+1]` - also the loop runs till n. Not to mention your loop runs till `i = n` instead of `i = n - 2`. – kiner_shah Feb 01 '22 at 10:18
  • 1
    Note that `a[i]= a[i+1];` while running up to `n` results in undefined behaviour. – sagi Feb 01 '22 at 10:20
  • 1
    Accessing out of bounds array elements results in undefined behaviour. Take a pancil and a piece of paper and simulate the delettion of an element in an array with 4 elements. – Jabberwocky Feb 01 '22 at 10:21
  • 1
    You are seeing the value in unitialized memory. – the busybee Feb 01 '22 at 10:22
  • 1
    (1) `n-1` is the last element. (2) When an element is deleted, the number of defined elements is decreased by 1. – nielsen Feb 01 '22 at 10:22
  • 2
    Also, if you don't reallocate the array, what exactly do you expect the last cell to hold? Your array is still of the same size after the "deletion" – sagi Feb 01 '22 at 10:22

1 Answers1

0

I modified your code , and as they were saying in the comments for your question , the problem is that you were accessing elements outside you array range , also there is one more thing with you code is the what if you want to delete the last element in the array , how would you delete it ? , instead of shifting all elements of the array , you can put the maximum/minimum value of integer in the place you are deleting so that you come back to that place , you will know this place was deleted before . this will decrease the time of deletion (O(1))but it will increase the time of insertion (O(N)), wile you code is based on increasing the time of deletion (O(n)) but it decreases the time of insertion (O(1)) this is your modified code based on shifting on deletion :

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int arr[30];
   int n, i, pos;

   printf("how many ele\n");
   scanf_s("%d", &n);
   printf("enter %d ele\n", n);
   for (i = 0; i < n; i++)
   {
       scanf_s("%d", &arr[i]);
   }

   printf("enter position of element to be deleted\n");
   scanf_s("%d", &pos);
   for (i = pos - 1; i < n - 1; i++)
   {
       arr[i] = arr[i + 1];
   }
   printf("elements in array are\n");
   for (i = 0; i < n - 1; i++)
   {
        printf("%d\t", arr[i]);
   }
   return 0;
}
abdo Salm
  • 1,678
  • 4
  • 12
  • 22
  • Shouldn't the last loop run till `i = n - 2`? Also is `scanf_s` part of Standard C? I think it's a special function provided by [Microsoft compiler](https://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s). – kiner_shah Feb 02 '22 at 04:14
  • yes exactly ,it is the same if you write `for (i = pos - 1; i < n - 1; i++)` , it's the same as if write `for (i = pos - 1; i <= n - 2; i++)` ; for `scanf_s` , it's not part of C standard , but my visual studio compiler complains about `scanf` , but it should be `scanf` as you said – abdo Salm Feb 02 '22 at 08:50
  • 1
    oh , you meant the last loop , you are right . it must be till i = n - 2 , that's my bad – abdo Salm Feb 02 '22 at 09:00