Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rayshar
    New Member
    • Feb 2008
    • 2

    Arrays

    Hi, i need to write a program that inserts and deletes an element and dont know how to go about it, please assist
  • sanctus
    New Member
    • Mar 2007
    • 84

    #2
    Originally posted by Rayshar
    Hi, i need to write a program that inserts and deletes an element and don't know how to go about it, please assist
    Well, you create an array of integers of say 10 elements with
    Code:
    int a[10];
    you fill it with commands like
    Code:
    a[3]=2;
    So the forth (starts counting at zero) element is 2.

    I don't see why you need to delete an element, you just can give it another value (as long as it is not defined as constant). In case typing
    Code:
    a[3]='\0';
    seems to work but I don't guarantee that (I'm not sure about it)

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      To insert an element into an array you need to make sure that the array size is at least 1 element larger than the number of elements it currently holds then assign the value to the n-1 element.
      if you mean to insert a value into an element in the correct order the array must be sorted in assending order. Then each element of higher value than the one to be inserted has to be moved one place up. Then the value can be inserted at the correct element location to preserve the order. For loops are usually used to control this activity

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by whodgson
        To insert an element into an array you need to make sure that the array size is at least 1 element larger than the number of elements it currently holds then assign the value to the n-1 element.
        As long as that is where you want to put the element.
        Originally posted by whodgson
        if you mean to insert a value into an element in the correct order the array must be sorted in assending order. Then each element of higher value than the one to be inserted has to be moved one place up. Then the value can be inserted at the correct element location to preserve the order. For loops are usually used to control this activity
        Once again, that depends entirely on what you want to do. For some cases "correct" may be random, or descending order.

        Comment

        Working...