Thread: arrays

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    Unhappy arrays

    Hi,

    I wanted to know how do you insert and delete using an array?
    I think you use scanf but i dont know how?

    thanks

    kendals
    Kendals

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I wanted to know how do you insert and delete using an array?
    With great difficulty. When you insert into an array you need to allocate enough memory for a new element, or if the array is large enough you can skip this step. To insert you need to shift all of the elements from the point that you want to insert at one place over, thus clearing the element you want to insert to. Make your assignment and you're done with the insertion.

    To delete it's the opposite, clear the element and shift all elements past the one you cleared so that they move in to fill the empty space. These are two very intensive operations and you would really be better off with a linked list if you want insertion and deletion functionality.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Yeah, it's a pain in the @ss. Just move to MFC.. Windows has a class called CArray that will do all that crap for you.

    -Max

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just move to MFC
    What fun is having everything done for you? Especially if you aren't familiar with the language to begin with, if you learn something like MFC without knowing the underlying fundamentals of how it works then you're only hurting yourself.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    do you have an example?

    Hi do you have an example of that insert and delete?

    thanks

    kendals

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    lol. it was a <a href="www.dictionary.com">joke</a> [dictionary.com]

    as for inserting and deleting elements in an array:

    it's really a pain in the butt... here's the basic logic, I'll leave the implementation up to you.

    if you wish to remove an element, eg delete it, you have to empty the contentents of the array at that point, or simply write over them..... then you have to move *every* element in the array after the element deleted one "back" in the array, so that everything is all nice and pretty....

    adding an element, eg inserting, is essentially the reverse of this process, but you also need to check that you can write to the array-- eg, you're not trying to write past the end of it.

    that's why i'm fond of the CArray class in MFC... it takes care of all that grunt work for you... the downside of it is that you have to include a huge header file in your program that will inflate the exe something huge... (and by huge i mean to like 100k or so...)

    An easier way to create a dynamic array, since this is essentally what you want, is to use a linked list. Check out the links on this site for how they work.

    -Max

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM