944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1995
  • C++ RSS
Nov 29th, 2007
0

dynamic array help

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quickster12 is offline Offline
2 posts
since Nov 2007
Nov 30th, 2007
0

Re: dynamic array help

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 30th, 2007
0

Re: dynamic array help

I keep gettin an unexpected end of file error.


c++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. #include "IVec.h"
  5.  
  6. typedef int Capacity;
  7. class IVector
  8. {
  9. private:
  10. Capacity * items;
  11. int count ;
  12.  
  13. public:
  14. IVector(int Count, int cap)
  15. {
  16. {int Capacity = 2;
  17. count = 0;
  18. }
  19. { [Capacity] = cap;
  20. count = 0;
  21. }
  22. void IVector (const IVector & V )
  23. {
  24. cout << " The first value is ... " << V << endl;
  25. }
  26. void Append( int item )
  27. { count = Count;
  28. items = new Capacity [count];
  29. for ( int i = 0; i < count; i++)
  30. items [i] = 0;
  31. }
  32. int Count( ) { return count; }
  33. {
  34. Capacity & operator[]( int index ) { return items[index]; }
  35. }
  36. void Insert (int index, int item )
  37. {item = items;
  38. for (index = 0; index < count; index++)
  39. cout << [index] << " ";
  40. cout << endl;
  41. }void Delete (int index )
  42. {
  43. delete item = index;
  44. for ( index = 0; index < count; index--)
  45. }
  46. };
  47. void ShowIVector( string label, IVector & v )
  48. {
  49. cout << "\n " << label << ' ';
  50. for ( int i = 0; i < v.Size(); ++i )
  51. cout << ' ' << v[i];
  52. cout << endl;
  53. }
  54. int _tmain(int argc, _TCHAR* argv[])
  55. { IVector v1(20);
  56.  
  57. cout << "\n v1.Size() = " << v1.Size() << endl;
  58.  
  59. ShowIVector( "v1 = ", v1 );
  60.  
  61. for ( int i = 0; i < v1.Size(); ++i )
  62. v1[i] = i;
  63.  
  64. ShowIVector( "v1 = ", v1 );
  65.  
  66. cout << "\n Press [Enter] to quit ... ";
  67. cin.get( );
  68. return 0;
  69. }
  70. }
Last edited by Ancient Dragon; Nov 30th, 2007 at 1:56 am. Reason: add line numbers to existing code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quickster12 is offline Offline
2 posts
since Nov 2007
Nov 30th, 2007
0

Re: dynamic array help

Glad to see you are posting this question everywhere you can
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 30th, 2007
0

Re: dynamic array help

line 10: you don't do math such as multiplication in the section that declares variables. You probably want to do this:
C++ Syntax (Toggle Plain Text)
  1. int Capacity;
  2. int * items;
  3. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 30th, 2007
0

Re: dynamic array help

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error with pointers
Next Thread in C++ Forum Timeline: simple C++ question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC