| | |
dynamic array help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
Ok im one of those people that are just completely unable to pick up C++. Im forced to take this class and i have no prior programmin experience but at least theres only a few weeks left of this. Anyway, I just want help with this assingnment. I'm not lookin for anyone to do this for me i just want to be walkedthrough with this, cuz i will need to do somethin like this for the final. Here's the assingment verbatim and nobody needs to help me with the extra credit part i just need to get the base assinment done. Thanks.
The Base Assignment
Implement and thoroughly test a class named IVector that represents a
dynamic array of integers. It will have 3 private data members: int
capacity, int count, and int * items. The 'capacity' is the physical size
of the dynamic array (its actual number of elements). The 'count' is
the number of elements currently in use (indices 0, 1, ..., count-1).
The pointer 'items' points to the first element of the dynamic array,
which will be created by the operator 'new'.
Class IVector will have three constructors: 1) a default constructor
that creates an array of capacity = 2 and count = 0; 2) a constructor
with parameter int cap that creates an array of capacity = cap and count =
0; and 3) a copy constructor with parameter "const IVector & V" that
creates an array that is identical to IVector V.
Class IVector will have the following public member functions: 1) two
getters that return the capacity and the count of an IVector object; 2)
one declared "void Append( int item )" that adds 'item' to 'items' at
position 'count' and increments 'count' by one; 3) one declared "void
Insert( int index, int item ) "that adds 'item' to 'items' at position
'index' and increments 'count' by one; and one declared "void Delete(
int index )" that deletes the item at position 'index' and decrements
'count' by one.
Overload the operator '[ ]' to access elements of the vector by
subscript.
Extra Credit
The following extra credit items are optional. You can get a "100"
without doing them.
1) Add two private member functions declared "void Grow( )" and "void
Shrink( )". function Grow() doubles the capacity of the array 'items'
without losing any element values and function Shrink() halves the
capacity of the array 'items' without losing any element values. Call
Grow() whenever and wherever count == capacity and methods Append() or
Insert() have been called. Call Shrink() whenever and wherever count <=
capacity/4 and method Delete() has been called.
2) Overload the assignment operator '=' to let you write v2 = v1; to
copy vector v1 over v2, thus making v2 identical to v1. This, as
expected, will lose any data v2 originally held.
The Base Assignment
Implement and thoroughly test a class named IVector that represents a
dynamic array of integers. It will have 3 private data members: int
capacity, int count, and int * items. The 'capacity' is the physical size
of the dynamic array (its actual number of elements). The 'count' is
the number of elements currently in use (indices 0, 1, ..., count-1).
The pointer 'items' points to the first element of the dynamic array,
which will be created by the operator 'new'.
Class IVector will have three constructors: 1) a default constructor
that creates an array of capacity = 2 and count = 0; 2) a constructor
with parameter int cap that creates an array of capacity = cap and count =
0; and 3) a copy constructor with parameter "const IVector & V" that
creates an array that is identical to IVector V.
Class IVector will have the following public member functions: 1) two
getters that return the capacity and the count of an IVector object; 2)
one declared "void Append( int item )" that adds 'item' to 'items' at
position 'count' and increments 'count' by one; 3) one declared "void
Insert( int index, int item ) "that adds 'item' to 'items' at position
'index' and increments 'count' by one; and one declared "void Delete(
int index )" that deletes the item at position 'index' and decrements
'count' by one.
Overload the operator '[ ]' to access elements of the vector by
subscript.
Extra Credit
The following extra credit items are optional. You can get a "100"
without doing them.
1) Add two private member functions declared "void Grow( )" and "void
Shrink( )". function Grow() doubles the capacity of the array 'items'
without losing any element values and function Shrink() halves the
capacity of the array 'items' without losing any element values. Call
Grow() whenever and wherever count == capacity and methods Append() or
Insert() have been called. Call Shrink() whenever and wherever count <=
capacity/4 and method Delete() has been called.
2) Overload the assignment operator '=' to let you write v2 = v1; to
copy vector v1 over v2, thus making v2 identical to v1. This, as
expected, will lose any data v2 originally held.
It's hard to know what help to give, where to point you, without knowing how much you can accomplish on your own.
Please make a start on the assignment and post it here, then we can figure out what aid you need.
Val
Please make a start on the assignment and post it here, then we can figure out what aid you need.
Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
I keep gettin an unexpected end of file error.
c++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> using namespace std; #include "IVec.h" typedef int Capacity; class IVector { private: Capacity * items; int count ; public: IVector(int Count, int cap) { {int Capacity = 2; count = 0; } { [Capacity] = cap; count = 0; } void IVector (const IVector & V ) { cout << " The first value is ... " << V << endl; } void Append( int item ) { count = Count; items = new Capacity [count]; for ( int i = 0; i < count; i++) items [i] = 0; } int Count( ) { return count; } { Capacity & operator[]( int index ) { return items[index]; } } void Insert (int index, int item ) {item = items; for (index = 0; index < count; index++) cout << [index] << " "; cout << endl; }void Delete (int index ) { delete item = index; for ( index = 0; index < count; index--) } }; void ShowIVector( string label, IVector & v ) { cout << "\n " << label << ' '; for ( int i = 0; i < v.Size(); ++i ) cout << ' ' << v[i]; cout << endl; } int _tmain(int argc, _TCHAR* argv[]) { IVector v1(20); cout << "\n v1.Size() = " << v1.Size() << endl; ShowIVector( "v1 = ", v1 ); for ( int i = 0; i < v1.Size(); ++i ) v1[i] = i; ShowIVector( "v1 = ", v1 ); cout << "\n Press [Enter] to quit ... "; cin.get( ); return 0; } }
Last edited by Ancient Dragon; Nov 30th, 2007 at 1:56 am. Reason: add line numbers to existing code tags
Glad to see you are posting this question everywhere you can
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
line 10: you don't do math such as multiplication in the section that declares variables. You probably want to do this:
lines 16-21 don't make sense, and the function has no closing brace.
C++ Syntax (Toggle Plain Text)
int Capacity; int * items; int count ;
lines 16-21 don't make sense, and the function has no closing brace.
Last edited by Ancient Dragon; Nov 30th, 2007 at 2:01 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
That error is usually because some function is not closed off - curly braces are out of sync.
You need to work on formatting, that will help to spot such problems. And, it looks like you have some functions defined within other functions, which is illegal.
Val
You need to work on formatting, that will help to spot such problems. And, it looks like you have some functions defined within other functions, which is illegal.
Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- works for static need help to make it dynamic (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: error with pointers
- Next Thread: simple C++ question
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






