I am having problems with the most important section of this program. I must admit after reading, I understand the concept, but applying it to this particular program is not coming through. I am attaching the source code but the portion I am having problems with is the following:

void add_item(double number)

What I am trying to do is also attached in the sample snapshot. I know what I need to do but have a hard time implementing. I know I should add entries until it reaches the max. Once it is at the max, add the next one but double the maximum. I hope someone can help.

Attachments
//Filename:  Assignment6B1.cpp
//Author:  Gregory Speights
//Assignment Number:  6B1
//Description:  
//Last Changed:  August 20, 2005

#include <iostream>
#include <cstdlib>

using namespace std;


const int MAX_LIST_SIZE = 50;

typedef double* ArrayPtr;

// ==========================
//   List class declaration
// ==========================
class List
{
public:
  List();
  // Initializes the List object to an empty list with size MAX_LIST_SIZE

  List(int maxSize);
  // Initializes the List object to be a list with at most
  // maxSize entries.

  void add_item(double number);
  // Precondition: number has a value
  // Postcondition: The argument number has been added to
  // the list. If the list was full before adding, its maximum
  // size is double what it was before adding.

  bool full() const;
  // Returns true if the list if full; false otherwise.

  friend ostream& operator <<(ostream& outs, const List& theObject);
  // Overloads the << operator so it can be used to output
  // values of type List.
  // Precondition: If outs is a file stream then outs has
  // already been connected to a file.

  int get_size() const;
  // Returns the number of values in the list.

  int get_max() const;
  // Returns the maximum number of entries in the list.

  double get_last ();
  // Precondition: The List object is defined and non-empty.
  // Returns the last value in the list.

  void delete_last();
  // Precondition: The List object is defined.
  // Postcondition: If the list was nonempty, the last item has
  // been deleted from the list array and size has been decremented;
  // otherwise the list remains empty. If deletion of the item
  // caused the size of the list to drop below half the maximum,
  // then the maximum is reduced to half the original and the
  // amount of space used to store the array is half what it was.


private:
  ArrayPtr list;
  int max;   // maximum number of elements
  int size;  // number of array positions filled
};

// ======================
//     main function
// ======================
int main()
{
  //
  // Variable declarations
  // 
  double num;        // a number to be added to the list
  char ans;          // loop control
  int size;

  cout << endl << "Testing the List Class" << endl << endl;

  //
  // Get the maximum number of entries and create a List object
  //
  cout << "What is the maximum number of entries in the list? ";
  cin >> size;
  cout << endl;

  List testList(size);

  //
  // Let the user test add_item, delete_last, and get_last as long
  // as he/she wishes
  //
  do 
    {
      cout << endl;
      cout << "Enter a to add to the list, d to delete the last entry, " << endl;
      cout << "p to print the last item (without deleting), or s to stop: "; 
      cin >> ans;
      
      if (ans == 'a' || ans == 'A')
	{
	  //
	  // Get the number to be added, add it, then output the list
	  //
	  cout << "Enter a number to add to the list: ";
	  cin >> num;
	  testList.add_item(num);
	}
      else if (ans == 'd' || ans == 'D')
	testList.delete_last();
      else if (ans == 'p' || ans == 'P')
	cout << endl << "The last item in the list is " 
	     << testList.get_last() << endl;
      else if (ans != 's' && ans != 'S')
	cout << "Not a valid response - try again!" << endl;  
      
      cout << endl << "Current List" << endl;
      cout << "Capacity: " << testList.get_max() << " Size: "
	   << testList.get_size() << endl;
      cout << "List entries: " << testList << endl;
      
    }
  while (ans != 's' && ans != 'S');
  
  return 0;
}

// ============================
//    function definitions
// ============================

List::List()
{
  size = 0;
  max = MAX_LIST_SIZE;
}


List::List (int maxSize)
{
  max = maxSize;
  size = 0;
  list = new double[maxSize];
}

void add_item(double number)
{
	int a, i;
	if (i > 50)
	{
		int time[50];
		int *a;
		a = new int [time];	
		int *p = a;
		int i;
		for (i = 0; i < time; i++)
			a[i] = i;
	}
	else
	{
		size = size * 2
		a[i] = i;
	}
	
}


bool List::full() const
{
  return (size == max);
}


ostream& operator <<(ostream& outs, const List& theObject)
{
  for (int i = 0; i < theObject.size; i++)
    outs << theObject.list[i] << "  ";
  return outs;
}


int List::get_size() const
{
  return size;
}


int List::get_max() const
{
  return max;
}

In your solution void add_item(double number) should be a member of the class.

It should be defined using

void List::add_item(double number) outside the class

Also I noticed that you never use the number parameter within the add_item method.

Does your progam compile?

-------------------------
Programming ( Assignment / Project ) Help

in the default constructure allocate the array with max number of doubles.

function add_item is all f**ked up. change the name to list::add_item and delete all its contents.

if size == MAX_LIST_SIZE then reallocate the array, otherwise just insert the new number into list[size++];

void list::add_item(double number)
{
    ArrayPtr *m = new ArrayPtr double [d1];
    delete [m];
    int i, d1, d2;

    if size = MAX_LIST_SIZE
    {
        for (i = 0; i < d1; i++)
            m[i] = new int[d2]

    }
    else
    {
        list [size] = number;
        size++;
    }
}

I'm not getting it. Once I check the size, add it and then count.....else just add the record???

Edited 3 Years Ago by pyTony: fixed formating

void list::add_item(double number)
{
    if (size == max )
    {
               // reallocate the array 
               ArrayPtr m = new ArrayPtr [max + MAX_LIST_SIZE];
              // copy existing data into new array
               memcpy(m, list, size * sizeof(double));
              // delete old array object
              delete[] list;
              // reset list
              list = m;
              // bump array size
              max += MAX_LIST_SIZE;
        }
       // add number to the list
    list [size] = number;
    size++;
}

I'm not getting it. Once I check the size, add it and then count.....else just add the record???

Edited 3 Years Ago by diafol: fixed formatting

This article has been dead for over six months. Start a new discussion instead.