Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   help with small c++ assignment (http://www.velocityreviews.com/forums/t457603-help-with-small-c-assignment.html)

KamilCzauz@gmail.com 10-12-2006 11:31 PM

help with small c++ assignment
 
hey guys,

i have a small assignment due in c++ and im having some trouble. We
are supposed to make a program that takes in a N number of strings and
sorts them into a dynamic array as the strings are being inputed(sort
on the fly).

>From what i understand this requires that a new array must be created

everytime a new string is inserted. I dont really understand this
because the entire program depends on the old array. How can i make
this new array replace the old array?

Im not sure if any of this sounds clear, but here is my assignment
below, any help would be greatly appreciated.

Thanks,
Kamil




* When you initially populate the List, add the strings in a sorted
way.
* For the method insertString, do the following:
o Allocate a new array, one greater than the current size of
the array.
o Determine the position of the new string in the new array.
The strings should be entered so that they are sorted based on their
length. If two strings are of equal length, you can insert the new
string before or after the one that is of equal length.
o Copy all the elements of the current array into the new
array, and make sure the new string is inserted at the appropriate
position.
o delete the old array. (Remember to use the syntax "delete
[] arrayName".
o the dynamic array pointer of the List (data member) should
now point to the newly allocated array.
* For the method removeString(), allocate a new array
o Allocate a new array, one smaller than the current size of
the array.
o Copy all the elements of the current array into the new
array (except the one that needs to be removed).
o delete the old array. (Remember to use the syntax "delete
[] arrayName".

ListMain.cpp requirements

* store the following strings in a list: "DATA
STRUCTURES","CONSTRUCTORS", "DESTRUCTORS", "TEMPLATES", "COPY
CONSTRUCTOR", "EXPLICIT VALUE CONSTRUCTOR", "MEMORY LEAKS", "A QUICK
BROWN FOX", "JUMPS OVER THE LAZY DOG".
* then iterates through the list to display all the strings stored
in the list.
* then call removeString to remove the following strings from the
list: "MEMORY LEAKS", "TEMPLATES". Note that removeString should follow
the instructions listed above.
* then iterates through the list again to display all the strings
stored in the list.
* then insert strings ("ADT", "OVERLOAD", "OPERATOR") in the List
using the method insertMethod based on the requirements listed above.


Gianni Mariani 10-13-2006 12:53 AM

Re: help with small c++ assignment
 
KamilCzauz@gmail.com wrote:
> hey guys,
>
> i have a small assignment due in c++ and im having some trouble. We
> are supposed to make a program that takes in a N number of strings and
> sorts them into a dynamic array as the strings are being inputed(sort
> on the fly).
>
>>From what i understand this requires that a new array must be created

> everytime a new string is inserted. I dont really understand this
> because the entire program depends on the old array. How can i make
> this new array replace the old array?
>
> Im not sure if any of this sounds clear, but here is my assignment
> below, any help would be greatly appreciated.



For "dynamic arrays" look at std::vector - e.g.

#include <vector>
#include <string>

int main()
{

std::vector<std::string> dyn_array( 3 );

dyn_array[0] = "String 1";
dyn_array[1] = "String 2";
dyn_array[2] = "String 3";

std::vector<std::string> another_dyn_array;

another_dyn_array = dyn_array; // copy the contents of one
// vector to another.

// look at std::sort

}

>
> Thanks,
> Kamil
>
>
>
>
> * When you initially populate the List, add the strings in a sorted
> way.
> * For the method insertString, do the following:
> o Allocate a new array, one greater than the current size of
> the array.
> o Determine the position of the new string in the new array.
> The strings should be entered so that they are sorted based on their
> length. If two strings are of equal length, you can insert the new
> string before or after the one that is of equal length.
> o Copy all the elements of the current array into the new
> array, and make sure the new string is inserted at the appropriate
> position.
> o delete the old array. (Remember to use the syntax "delete
> [] arrayName".
> o the dynamic array pointer of the List (data member) should
> now point to the newly allocated array.
> * For the method removeString(), allocate a new array
> o Allocate a new array, one smaller than the current size of
> the array.
> o Copy all the elements of the current array into the new
> array (except the one that needs to be removed).
> o delete the old array. (Remember to use the syntax "delete
> [] arrayName".
>
> ListMain.cpp requirements
>
> * store the following strings in a list: "DATA
> STRUCTURES","CONSTRUCTORS", "DESTRUCTORS", "TEMPLATES", "COPY
> CONSTRUCTOR", "EXPLICIT VALUE CONSTRUCTOR", "MEMORY LEAKS", "A QUICK
> BROWN FOX", "JUMPS OVER THE LAZY DOG".
> * then iterates through the list to display all the strings stored
> in the list.
> * then call removeString to remove the following strings from the
> list: "MEMORY LEAKS", "TEMPLATES". Note that removeString should follow
> the instructions listed above.
> * then iterates through the list again to display all the strings
> stored in the list.
> * then insert strings ("ADT", "OVERLOAD", "OPERATOR") in the List
> using the method insertMethod based on the requirements listed above.
>


David Harmon 10-13-2006 02:08 AM

Re: help with small c++ assignment
 
On 12 Oct 2006 16:31:41 -0700 in comp.lang.c++, KamilCzauz@gmail.com
wrote,
>>From what i understand this requires that a new array must be created

>everytime a new string is inserted. I dont really understand this
>because the entire program depends on the old array. How can i make
>this new array replace the old array?


The dynamic array is referred to only by a pointer pointing to the
dynamically allocated memory. The rest of the program depends only
on that pointer, and goes straight to the new array when the pointer
changes. How to exchange the arrays is spelled out in extreme
detail in your assignment, as quoted below.


>* For the method insertString, do the following:
> o Allocate a new array, one greater than the current size of
>the array.
> o Determine the position of the new string in the new array.
>The strings should be entered so that they are sorted based on their
>length. If two strings are of equal length, you can insert the new
>string before or after the one that is of equal length.
> o Copy all the elements of the current array into the new
>array, and make sure the new string is inserted at the appropriate
>position.
> o delete the old array. (Remember to use the syntax "delete
>[] arrayName".
> o the dynamic array pointer of the List (data member) should
>now point to the newly allocated array.


"should now point to" = you assign the new pointer value to it.


Stuart Redmann 10-13-2006 07:46 AM

Re: help with small c++ assignment
 
KamilCzauz@gmail.com wrote:
> hey guys,
>
> i have a small assignment due in c++ and im having some trouble. We
> are supposed to make a program that takes in a N number of strings and
> sorts them into a dynamic array as the strings are being inputed(sort
> on the fly).
>
>>From what i understand this requires that a new array must be created

> everytime a new string is inserted. I dont really understand this
> because the entire program depends on the old array. How can i make
> this new array replace the old array?


As you mentioned above, you should do this in C++. So you probably
should define a class that looks like the following:

class CSortedStringList
{
public:
void insertString (std::string p_NewString);
void removeString (std::string p_ToBeDeleted);
std::string GetString (int Position);
private:
<< your implementation details here >>
};

Your string class should be implemented by using an array of strings, so
your implementation will look like this:
std::string* m_pStringArray;

Whenever you insert or delete a string from your list you'll have to
create a new array and copy the values from m_pStringArray accordingly.
Then you assign the location of the copy to m_pStringArray. Keep in
mind, that noone can access the private member m_pStringArray directly,
so you can replace it as you like. The only access to the internal
string array should be possible through the public methods like
GetString. That's what object-orientation is (mainly) all about.

Regards,
Stuart

PS: Although Gianni posted a elegant solution to this problem, I doubt
that your instructor would be happy with it (since the assignment is
obviously made to get you acquainted with object-orientation and memory
management). If you can spare the time, I'd advise you to have a look at
Gianni's solution, so that you may get a better view of how things are
done the proper way under C++.


All times are GMT. The time now is 02:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57