Thread: Delete function to remove an element from array not working

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    12

    Delete function to remove an element from array not working

    I'm trying to create a delete function that accept an index to delete and return the value stored at the index to the user after it has been deleted from the array, but when I ran the program and try to delete an element the value is remain in the array.

    Here is my code:
    Code:
    void deleteFunc(struct Array *myArr, int x ){
    
    
        int i, size;
        if(myArr -> length == 0)
         {
          printf("Value not found is in Array \n\n");
          return ;
         }
         if(x >= 0 && x < myArr -> length){
            for(i = x; i < myArr -> length -1; i++){
                myArr -> A[i] = myArr -> A[i+1];
            }
            myArr -> length--;
            
          printf("Array before deletion: ");     
          for(i = 0; i < size; i++){
            printf("%d", myArr[i]);
          }
    
    
         printf("Array after deletion: ");
         for(i = 0; i < size; i++){
            printf("%d", myArr[i]);
        }
    
    }
    Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    Your loop to "delete" the element is concerned with myArr->A[i], whereas your loop to print the array is concerned with myArr[i]. You need to decide which array you're working on. The reason it looks like there's no effect is because you're operating on one array and then printing a different array.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete element of array
    By Anders Ekdahl in forum C Programming
    Replies: 1
    Last Post: 11-01-2015, 12:15 AM
  2. remove array element
    By Ash Mo Naiming in forum C Programming
    Replies: 6
    Last Post: 01-30-2013, 09:57 AM
  3. HELP > How to remove element in a Structure Array
    By Inneart in forum C Programming
    Replies: 5
    Last Post: 10-26-2010, 02:40 PM
  4. delete same array element in C
    By tasosa in forum C Programming
    Replies: 12
    Last Post: 04-09-2009, 09:36 AM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM

Tags for this Thread