Appending elements to dynamic array

Hewlett-packard / Fq568aa-aba s3700f
March 11, 2009 at 17:43:06
Specs: Microsoft Windows Vista Home Premium, 2.6 GHz / 2941 MB
I have to create a function called append, it will be passed a value, and the value will be added to the end of the array if theres room. if added the function returns true. if not it returns false. how can i do this. below is the current header of the class. what would the prototype look like. thanks for any help.

#include <iostream>
using namespace std;

#ifndef INTEGERLIST_H
#define INTEGERLIST_H

class integerList
{
public:
integerList(); //Default constructor, 0 size array
integerList(int sz); //Constructor create array and initialize values to 0
~integerList(); //Destructor

void create(int sz); //Allocates the array and initializes value to 0
void clear(); //Removes the array space
int getSize() const; //Returns the numElements
bool setElement(int loc, int value); //If loc valid, assigns value at index loc and
//Returns true, otherwise return false
bool getElement(int loc, int &value) const; //If loc valid, assign value from there and
//return true, otherwise return false
private:
int *list; //Pointer to array
int numElements; //Number of elements

bool isValid(int loc) const; //Private function used to make sure the loc is in range
};

ostream &operator<<(ostream &stream, const integerList &list);

#endif


See More: Appending elements to dynamic array

Report •


#1
March 11, 2009 at 22:16:02
I have to create a function called append,
append();

it will be passed a value,

append(int value);

the function returns true . . . false.

bool append(int value);


Report •

#2
March 12, 2009 at 09:55:16
Also,

#include <iostream>

can be replaced with

#include <iosfwd>

for better efficiency.

And

using namespace std;

is a bad idea in a header file.


Report •

#3
March 12, 2009 at 10:31:31
Thanks guys, but I don't understand the actual code in the
fuction that will add elements to my dynamic array.

the given prototype is

integerList::appendElements(integerList &array)

thanks.


Report •

Related Solutions


Ask Question