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
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
>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.
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
>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.
Hi do you have an example of that insert and delete?
thanks
kendals
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