GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Mar-2006, 15:55
headphone69 headphone69 is offline
New Member
 
Join Date: Mar 2006
Posts: 3
headphone69 is on a distinguished road

Need help deleting the last element in the array


I 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:

class array                                 // Array Class
{
     private:
             int size;
             int *arpt;
             bool extend ();
             bool contrat ();
     public:
             array();
             array (const array& ar);
             ~array ();
             bool append(int i);
             bool deletelast();
             bool print();
             bool sort();
};

array :: array ()
{
     *arpt = 0;
}

array :: array (const array& ar)
{
     size = 1;
     ar = new int [size];
     *(ar + 0) = 10;
}

array :: ~array()
{
     delete[] arpt;
}


bool array :: append(int i)
{
    size ++;
    
    temp = new int(size);
    
    for ( i = 0; i < size -1; i++)
    
    *(temp+1)= *(arpt+i);
    
    delete [ ] arpt;
    
    arpt = temp;
    *(temp+ size - 1) = i;
    
}

bool array :: delete last()
{
    size--;
    
    temp = new int [size]
    for  (i = 0; i < size -1; i--)


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
      int i;
  array one;
  array two;
  one.printit();
  one.append(30);
  one.printit();
  one.append(20);
  one.printit();
  one.append(10);
  cout <<"Array before sorting:"<<endl;
  one.printit();
  one.sortit();
  cout << "Sorted Array:"<< endl;
  one.printit();
  one.deletelast();
  cout << " Array with last element deleted:"<< endl;
  one.printit();
  two = one;
  cout << "Second Array:"<< endl;
  two.printit();


   system("PAUSE");
   return EXIT_SUCCESS;
}


  #2  
Old 15-Mar-2006, 17:49
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: Need help deleting the last element in the array


Some observations:
  1. It seems the class skeleton was given to you, right? There are two private methods extend and contrat (should it be contract?) that you do not attempt to define or use.
  2. I think the idea here seems to be that you should define those private methods, and then your public append and deleteLast methods should be able to call them.
  3. Since delete is a keyword, why not call your method removeLast or eraseLast?
  4. Although the method for printing the array is called print, in your main function you call the method printit.
  5. Rather than post code that is missing some important pieces (I know you mentioned you're having trouble with those), why not try completing those functions, and if you run into compile errors, or runtime (logic) errors you can post specific questions abouwhat you don't understand?

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  
Old 15-Mar-2006, 19:31
headphone69 headphone69 is offline
New Member
 
Join Date: Mar 2006
Posts: 3
headphone69 is on a distinguished road

Re: Need help deleting the last element in the array


Hows this so far?



CPP / C++ / C Code:
class array                                 // Array Class
{
     private:
             int size;
             int *arpt;
             void extend ();
             void contract ();
     public:
             array();
             array (const array& ar);
             ~array ();
             bool append(int i);
             bool deletelast();
             bool print();
             bool sort();
};

array :: array ()
{
     size = 0;
     arpt = 0;
     
}

void array :: extend ()
{
    
    int *brpt;
    brpt = new int [size+1];
    
    for(int i = 0; i < size; i++ )
    
    *(brpt + i) = arpt[i];
    
    delete[] arpt;
    
    arpt = brpt;
}    

array :: ~array()
{
     delete[] arpt;
}

bool array :: append(int i)
{
    extend();   
    
    arpt [size] = i;
    size = size + 1;
}

void array :: contract ()
{
    int *brpt;
    brpt = new int [size-1];
    
    for(int i = 0; i < size; i++ )
    
    *(brpt + i) = arpt[i];
    
    delete[] arpt;
    
    arpt = brpt;
    
    size = size - 1;
}    
    
    

bool array :: delete last()
{
    contract();
}    



print array
    


#include <cstdlib>
#include <stdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
      int i;
  array one;
  array two;
  one.printit();
  one.append(30);
  one.printit();
  one.append(20);
  one.printit();
  one.append(10);
  cout <<"Array before sorting:"<<endl;
  one.printit();
  one.sortit();
  cout << "Sorted Array:"<< endl;
  one.printit();
  one.deletelast();
  cout << " Array with last element deleted:"<< endl;
  one.printit();
  two = one;
  cout << "Second Array:"<< endl;
  two.printit();


   system("PAUSE");
   return EXIT_SUCCESS;
}
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 23:06.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.