Thread: Insert element after the last element in array in c

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    22

    Insert element after the last element in array in c

    Hello everyone

    I have an array say int array[30]. I want to add elements to this array. New elements needs adding after the last element. Is there any simple way to do this?
    The structure of the code is somewhat as under
    Code:
    int main()
    {
    int array[30],x;
    scanf("%d",&x);
    
    //i need to traverse through the array till it reaches the last element (if there are any elements present and then add the new element after the last element
    //also it would be good to check that the size does not exceed 30
    return 0;
    }
    any suggestion would be appreciated.thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    Keep track of the number of elements currently in use, upon which you can access the last element in constant time and also trivially check that it does not exceed 30.
    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

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Hello Laserlight
    How r u?thanks for your reply

    Ok, so far I have managed to write the program as follows.

    The code creates an array, checks if the element to be added is already present in the array list.If not present added to the array otherwise prints out "id present".However, when I call the display function it displays the array 30 times.But i want to display only the elements that is present in the array. Thats it.How do i proceed from here.Also please suggest is the code is alright?

    Please help.Thank u

    Code:
    
    #include <stdio.h>
    #define MAX 30
    
    int find_dev(int a[],int data)
    {
       int i;
       for (i=0; i<MAX; i++)
       {
         if (a[i] == data)
         {
            return(data);
         }
       }
       return(-1); 
    }
     
    int add_id(int a[],int data)
    {
    int i;
    for(i=0;i<30;i++)
     a[i] = data;
    printf("ID added %d\n",a[i-1]);
    }
    
    int display(int a[])
    {
    int i;
    for(i=0;a[i]!=0;i++)  
    printf("ID added %d\n",a[i]);
    }
    
    int main()
    {
    int array[30]={};
    int i,ret;
    int data;
    
      while(1)
      {
        scanf("%d",&data);
        ret=find_dev(array,data);
        if(ret==-1)
          {
        add_id(array,data);
        display(array);
          }
          else
          {
        printf("ID already exists");
          }
      } 
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    The code creates an array, checks if the element to be added is already present in the array list.If not present added to the array otherwise prints out "id present".However, when I call the display function it displays the array 30 times.But i want to display only the elements that is present in the array. Thats it.How do i proceed from here.Also please suggest is the code is alright?
    Look at your "add_id()" function. You're not adding the data to the end of the array - you're overwriting the entire array with that value.

    laserlight suggested keeping track of the number of elements in use. If you go this route, then you should use a variable in "main()" to track this information and pass it to your other functions as needed (so you know what index the next value goes, and how much of the array to print).

    It appears that now, you're using a sentinel value of 0 to determine the end of valid data in the array. This approach works too, as long as zero is not considered valid data.

    Suggested improvements:

    • Be more consistent with your indentation.
    • The functions "add_id()" and "display()" are supposed to return an int, but are not returning anything. Either return a value, or make their return types void.
    • When passing an array to a function, it's good practice to also pass the size of the array
    • You define a constant that is used to iterate through the array in "find_dev()", but you're still using the magic number 30 in "add_id()" and "main()".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  2. Insert new element into a vector
    By appointment in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2009, 01:54 AM
  3. how to compare element in array
    By hlam in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2003, 12:05 AM
  4. insert an element i array
    By planet_abhi in forum C Programming
    Replies: 6
    Last Post: 02-22-2003, 07:04 PM
  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