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 24th, 2005, 09:43 PM
neo_darko neo_darko is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 neo_darko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 2 sec
Reputation Power: 0
Unhappy Multiple Max Element



Hi guys,

I am supposed to find the maximum element of an array. If there are more than one then get rid of all of them. And of course teh rest of the data is supposed to left shift. I can't work it out....

inpu : 78, 3, 45, 89, 34, 89, 2, 3, 12
ouyput : 78, 3, 45, 34, 2, 3, 12

Reply With Quote
  #2  
Old February 25th, 2005, 11:01 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey neo_darko!

Look, It occurs to me, that you could work out a recursive function (or a simple loop).
Code:
 
1) Get the biggest number 
2) search for it to check if there's more than one of them
	2.1) if found more than 1
	 2.1.1) delete them from the array (and shift the numbers)
	 2.1.2) Call function (recursive)
	2.2) else
	 2.2.1) Inform the array
 


IF I understood correctly, that's what you should do...the code, I leave it up to you. I only suggest you use some extra functions to get the number, search for it, and to erase it.

Good Luck...

Anibal.

Reply With Quote
  #3  
Old March 4th, 2005, 05:52 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
Quote:
Originally Posted by neo_darko


Hi guys,

I am supposed to find the maximum element of an array. If there are more than one then get rid of all of them. And of course teh rest of the data is supposed to left shift. I can't work it out....

inpu : 78, 3, 45, 89, 34, 89, 2, 3, 12
ouyput : 78, 3, 45, 34, 2, 3, 12


The bold text is not clear. You mean to remove elements with same values?

Reply With Quote
  #4  
Old March 4th, 2005, 07:33 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey Cirus!

I assumed that. I mean, for instances: the 89 is the maximum number, but since it apeares twice in the array, it must be removed. If another 78 was to be found, It would be removed as well. I think, it's like highlander: there can be only one (maximum)

Reply With Quote
  #5  
Old March 4th, 2005, 12:10 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
Quote:
Originally Posted by Anibal
Hey Cirus!

I assumed that. I mean, for instances: the 89 is the maximum number, but since it apeares twice in the array, it must be removed. If another 78 was to be found, It would be removed as well. I think, it's like highlander: there can be only one (maximum)


Hello Anibal.

One off-track question. If user gives a big array , supposingly of 1000 numbers, don't you think the algo will be time consuming. I mean suppose if two identical numbers appear, but their positions are far from each other. Say one occuring at 2nd position and next occuring at 998th position. With the current approach, lots of time will be wasted. Can you suggest a better method??

PS: Do not take my words otherwise. I am not saying wrong to our approach.

Reply With Quote
  #6  
Old March 4th, 2005, 12:15 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
I suggest to scan array using shell sort & bucket sort combination.
i.e: 1. Use shell sort to arrange numbers.
2. Re-sort the same using bucket sort.
3. Count the number of occurances of each number.If |bucket| > 1, remove
the number. GoTo step1.
Else
print result.

My approach will fail in case when each item is unique.At then lots of space will be wasted.

Any comments Anibal and others???????

Reply With Quote
  #7  
Old March 4th, 2005, 01:29 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey Cirus! Yes..the algorithm is in deed ineficient. The problem is that I don't know if you're allowed to sort the array. For what I saw, you're not allowed to do that. If you're not allowed to reorder the array, there's nothing left to do but what I have suggested.

If the array could be ordered, that's a different story! I would order the array with any method (each has it's own flaw...quicksort, bubblesort,...etc). Then, Remove those elements which are at the beginning of the array (should they be the biggest). That's as simple as it gets!.

I agree that eficient algorithms are allways best..but sometimes, given the processing power of todays computers (and the magnitud of the proyect, which happens to be a simple school asignment), it more a waste of time, to try and optimize it!.

Anyway, You were absolutly right about it and it's good that you presented an optimization of the algorithm.
Just a piece of advice (no ofense): a modification which causes the algorithm to fail in its ability to function in all cases for which it was designed, is not an optimization.
Nevertheless, thanks for the remark!!

Good Luck and keep it up!!

Anibal.

Reply With Quote
  #8  
Old March 5th, 2005, 12:02 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
Yes, I agree. But the algo. would perform better if there are repeated occurances of more than one items.

Thanks for discussing this.

Reply With Quote
  #9  
Old March 5th, 2005, 02:27 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Any time man! Discussing is the path to understanding and therefore....perfection!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Multiple Max Element


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway