Question: Manipulating Arrays

D

da Vinci

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? :)
 
R

Rolf Magnus

da said:
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.
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.
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.
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.
 
A

Acid_X

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 ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top