Thread: insert an element i array

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    insert an element i array

    How do i insert an element into array?
    pleae explain .
    AbHHinaay

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Last edited by Monster; 02-21-2003 at 08:00 AM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    1
    how to insert an element into an array

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Lightbulb how can i insert element in array

    this is the code to insert an element in to array
    -------------------------------------------------------------

    main()
    {
    int a[10],i,key,pos;

    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    printf("in which position u want to insert element: ");
    scanf("%d",&pos);
    scanf("%d",&key);/*element to insert*/
    for(i=n-1;i>=pos;i--)
    a[i+1] = a[i];
    a[pos] = key;
    n++;
    /* now u have the new element in the array*/
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    You do not need to alply the ampersand sign (&) to read into an array. Since an array is a form of a pointer the & sign is not needed.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Hmm..I was wrong. Funny how I have never come across that before but then again I never do use scanf() .

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The code doesn't even compile
    Code:
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    junk1.c:
    Error E2451 junk1.c 5: Undefined symbol 'n' in function main
    Warning W8065 junk1.c 5: Call to function 'scanf' with no prototype in function main
    Warning W8065 junk1.c 8: Call to function 'printf' with no prototype in function main
    Warning W8070 junk1.c 16: Function should return a value in function main
    *** 1 errors in Compile ***
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to insert a single value into every array alement?
    By philvaira in forum C++ Programming
    Replies: 11
    Last Post: 08-10-2008, 02:05 PM
  2. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM