![]() |
|
#1
|
|||
|
|||
Need help deleting the last element in the arrayI need help making a function that chops the array by deleting the last element and i have already started this function ( delete last() ), Then i need a function that prnts the values in the array and it would be much appriciated if you could check the rest of the program to make sure everything else flows and works right.
The assignment was to Define a class called array. The class simulates a dynmaic array of integers. The class should have two data members. The first data meber is the length of the array -- that is, the number of elements in it. The second data member is a pointer to an array that holds the data values. The array should have two private member functions: one that extends the array when an element is added and one that contracts it when an element s deleted. Should have the following functions: a. It should have one constructior that initializes the pointer to 0. b. It should have one logical copy constructor that copies and array. c. It should have one destructor that destroys the array. The destructor must delete the dynamic memory array. d. It should have one function that appends one integer at the end of the array. e. It should have one function that chops the array by deleting the last element. f. It should have one function that prints the values in the array. CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: Need help deleting the last element in the arraySome observations:
Also, unless it is a specific requirement of the program, it is very inefficient to allocate one new element at a time each time you call append, and to resize the array smaller each time you call deleteLast. I mean, suppose you deleteLast and then append, one after the other? In that case, your approach will have to allocate new memory, copy all elements but the last, delete the old memory, then allocate new memory again, copy all the elements, set the value for the last element, and delete the old elements again. Whew! A lot of unnecessary work. A typical approach for many dynamic containers (for example, std::vector of the C++ STL) uses two size variables, one for the current size, and one for the capacity (that is, max size). When you remove an element, you decrement the current size, but do not need to allocate new memory, and the capacity stays the same. When you append, you only need to allocate new memory if the new size will exceed the capacity. Of course, these considerations may seem to be too "advanced" if you're a beginner, but I think it's never to early to learn how to think through the problem and the design of your program. Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#3
|
|||
|
|||
Re: Need help deleting the last element in the arrayHows this so far?
CPP / C++ / C Code:
|
Recent GIDBlog
Match IP in CIDR by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Quick, Insertion, and Partition | silicon | C++ Forum | 0 | 18-May-2005 20:49 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Using an array and finding the element number (subscript) | tommy69 | C Programming Language | 27 | 05-Apr-2004 12:23 |
| Extra null element in an array | samtediou | MySQL / PHP Forum | 2 | 11-Dec-2003 11:52 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The