 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
da Vinci Guest
|
Posted: Wed Oct 29, 2003 12:17 am Post subject: Question: Manipulating Arrays |
|
|
OK, this is a pretty weird question, but shouldn't be to hard to
accomplish. Remember, I am a beginner so please, if I say something
stupid, be gentle. :-)
I do not know vectors yet, which would probably be a better idea than
an array for this, but I want to try using an array for now and then
will update it once I learn vectors. I havent really learned arrays
yet either.... :-)
So here it is.... I want to read in a string of characters into an
array. Then take that string of letters and remove all double +
letters. Meaning, you type in BLURRY and you end with BLURY (there
were two R's so one is removed). Then, I want to mix them all up.
So, three main stages. 1. Input 2. Remove doubles - tripples - ect.
and 3. Mix em up.
The input is pretty easy. I could use a for loop but then I would have
to have a set number of input characters, correct? I am using this
right now.....
char x = 'a';
int y = 0;
while ( a != '*' )
{
cin >> arrayname[y];
y++;
}
So they would input the characters until they were done and then add *
at the end. Then I can just discard the last entry into the array.
That will pull in my characters.
Now, no idea how I will remove like items. Maybe feed each element
into a new array and check for doubles as I go? Any good ideas here?
Then, how to mix them up.... I was thinking of using the srand and
rand to generate a random number based off an entered seed number and
grab that element number from the first array and insert it in order
in a new array.
So, how bad have I screwed up the logic on this one?
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Wed Oct 29, 2003 4:56 pm Post subject: Re: Question: Manipulating Arrays |
|
|
da Vinci wrote:
| Quote: | I do not know vectors yet, which would probably be a better idea than
an array for this, but I want to try using an array for now and then
will update it once I learn vectors. I havent really learned arrays
yet either....
|
If you don't know either, start with vectors.
Brian Rodenborn
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Oct 30, 2003 1:10 am Post subject: Re: Question: Manipulating Arrays |
|
|
da Vinci wrote:
| Quote: | OK, this is a pretty weird question, but shouldn't be to hard to
accomplish. Remember, I am a beginner so please, if I say something
stupid, be gentle. :-)
I do not know vectors yet, which would probably be a better idea than
an array for this, but I want to try using an array for now and then
will update it once I learn vectors. I havent really learned arrays
yet either....
|
Then you should better start with a vector and later go on to arrays.
| Quote: |
So here it is.... I want to read in a string of characters into an
array. Then take that string of letters and remove all double +
letters. Meaning, you type in BLURRY and you end with BLURY (there
were two R's so one is removed). Then, I want to mix them all up.
So, three main stages. 1. Input 2. Remove doubles - tripples - ect.
and 3. Mix em up.
The input is pretty easy. I could use a for loop but then I would have
to have a set number of input characters, correct? I am using this
right now.....
char x = 'a';
int y = 0;
while ( a != '*' )
{
cin >> arrayname[y];
y++;
}
So they would input the characters until they were done and then add *
at the end. Then I can just discard the last entry into the array.
|
You also have to check that y doesn't get bigger than the size of the
array - 1.
| Quote: | That will pull in my characters.
Now, no idea how I will remove like items. Maybe feed each element
into a new array and check for doubles as I go? Any good ideas here?
|
Possible. You could also do the same without copying to a new array,
i.e. you can reuse your array for the result.
| Quote: | Then, how to mix them up.... I was thinking of using the srand and
rand to generate a random number based off an entered seed number and
grab that element number from the first array and insert it in order
in a new array.
|
look up std::random_shuffle.
| Quote: | So, how bad have I screwed up the logic on this one?
|
|
|
| Back to top |
|
 |
Acid_X Guest
|
Posted: Sat Nov 01, 2003 3:08 pm Post subject: Re: Question: Manipulating Arrays |
|
|
On Wed, 29 Oct 2003 00:17:12 +0000, da Vinci wrote:
| Quote: | OK, this is a pretty weird question, but shouldn't be to hard to
accomplish. Remember, I am a beginner so please, if I say something
stupid, be gentle. :-)
I do not know vectors yet, which would probably be a better idea than
an array for this, but I want to try using an array for now and then
will update it once I learn vectors. I havent really learned arrays
yet either.... :-)
So here it is.... I want to read in a string of characters into an
array. Then take that string of letters and remove all double +
letters. Meaning, you type in BLURRY and you end with BLURY (there
were two R's so one is removed). Then, I want to mix them all up.
|
A linked list would probably be a more efficient data structure to use for
this task. Arrays and similar structures (like vectors) are great for
random access, but if you are going to be inserting and removing elements
frequently, some thing like a linked list is more efficient. The Standard
Library has a linked list in it. You might want to look into it ...
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|