Computing.Net > Forums > Programming > Appending elements to dynamic array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Appending elements to dynamic array

Reply to Message Icon

Name: nublet
Date: March 11, 2009 at 17:43:06 Pacific
OS: Microsoft Windows Vista Home Premium
CPU/Ram: 2.6 GHz / 2941 MB
Product: Hewlett-packard / Fq568aa-aba s3700f
Subcategory: C/C++
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: March 11, 2009 at 22:16:02 Pacific
Reply:

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);


0

Response Number 2
Name: klint
Date: March 12, 2009 at 09:55:16 Pacific
Reply:

Also,

#include <iostream>

can be replaced with

#include <iosfwd>

for better efficiency.

And

using namespace std;

is a bad idea in a header file.


0

Response Number 3
Name: nublet
Date: March 12, 2009 at 10:31:31 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Appending elements to dynamic array

c++ help on dynamic arrays www.computing.net/answers/programming/c-help-on-dynamic-arrays/5748.html

dynamic arrays in c++ www.computing.net/answers/programming/dynamic-arrays-in-c/5569.html

dynamic array www.computing.net/answers/programming/dynamic-array/2127.html