Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,269 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,463 people online right now. Registration is fast and FREE... Join Now!




Arrays: Deleting Repeated Strings

 
Reply to this topicStart new topic

Arrays: Deleting Repeated Strings

aznballerlee
5 Nov, 2006 - 08:43 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I'm trying to remove the consecutive strings from an array .. and I'm having difficulty coding it.

ex. "peter", "peter", "peter", "lois", "meg", "meg", "peter"
// would become "peter", "lois", "meg", "peter"

Here is my code:

CODE


for (i=n; i>0; i--)
{
    if (a[i] = a[i-1]
          a[i] = a[i-1]
}


but the problem with running this loop is that ..
i start from the end of the array, shift everything
to the left if there are repeats .. the problem is
that the ones shifted should be nullified (empty)
instead of still having the same string value .


anyone can help me accomplish this??
User is offlineProfile CardPM
+Quote Post

horace
RE: Arrays: Deleting Repeated Strings
6 Nov, 2006 - 12:00 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
when you delete an element from the array you need to shift the following elements down one and reduce the array size by one.

Why not use a vector which allows you to remove elements?
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Arrays: Deleting Repeated Strings
6 Nov, 2006 - 01:47 PM
Post #3

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I haven't learned vectors yet .. so I guess I can't use it. I think I have to stick with a for loop and some statements ..

This post has been edited by aznballerlee: 6 Nov, 2006 - 02:53 PM
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Arrays: Deleting Repeated Strings
6 Nov, 2006 - 02:53 PM
Post #4

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Task:

For every sequence of consecutive identical items in a,
remove all but one item of that sequence from a.
Return the number of items left in a.



I'm trying a new approach now ..

CODE


int removeDups(string a[], int n);
int removeDups(string a[], int n)
{

for (i = 0; i < n; ++i)
    {
        while ((a[i] == a[i+1])  
            shuffleDown (a, i+1, n);
    }

void shuffleDown (a[n], int index, int n)
{
     for (int i = index; i<n; ++i)
     {
         a[i] = a[i+1];
     }
        a[i-1] = "";
}

}


Would this work out??
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month