C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 29th, 2004, 10:59 PM
Ty* Ty* is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 2 Ty* User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ty* Send a message via MSN to Ty* Send a message via Yahoo to Ty*
c++, removing repeat entries in arrays

I know this is fairly simple, I just can't get it to work like I know it should. What I need is a function that removes repeat entries in an array.

Explanation/Example:

Code:
char a[10];

a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';

int size = 4;

removeRepeats(a, size);


In that example, removeRepeats() should detect the second 'a', remove it, and close the gap.

This is what I have so far for the function. It works for the example above, but not if the array is altered in any way.

Code:
void removeRepeats(char a[], int& size)
{
	int numReps = 0;
	int newsize;

	for(int i=0;i<size;i++)
	{
		for(int j=i+1;j<size;j++)
		{
			if(a[i] == a[j])
			{
				//a[j] = "";

				for(int k=j+1;k<size;k++)
				{
					a[k-1] = a[k];
					newsize = k;
				}

				numReps++;

				a[size-1] = a[size];
				
			}
		}
	}
	
	size = newsize;

	cout << "- Begin Array - size: " << size << endl;

	for(int l=0;l<size;l++)
	{
		cout << a[l] << endl;
	}

	cout << "- End Array -" << endl << endl;
	cout << "Number of repeats: " << numReps << endl;
}


What am I overlooking? Please help, thanks!

Ty

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > c++, removing repeat entries in arrays


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap