Finding Positon of characters

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 3
Reputation: Awais Ahmad is an unknown quantity at this point 
Solved Threads: 0
Awais Ahmad Awais Ahmad is offline Offline
Newbie Poster

Finding Positon of characters

 
0
  #1
Dec 2nd, 2008
I want to write a program which take input and save the poistions of the array elements in another array.
let if i enter an array
A[0]= 4
A[1]=1
A[2]=2
A[3]=3
now the output array will hold
1
2
3
0
i-e the posistions of the array elements starting from the smallest number.
As 1 is the smallest number and it is on 1 posistion the output array will hold values like this
B[0]=1 //Posistion of the smallest number
B[1]=2 //Posistion of second smallest number
B[2]=3 //and so on
B[3]=0 //as 4 is on 0 posistion so it will be on this location of out //array.
This is the problem any one to solve it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 30
Reputation: Ψmon is an unknown quantity at this point 
Solved Threads: 0
Ψmon Ψmon is offline Offline
Light Poster

Re: Finding Positon of characters

 
0
  #2
Dec 2nd, 2008
It seems as if the simplest way to affect a solution for your scenario would be to implement any of the available sorts, focusing on array index rather than array content.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is online now Online
Posting Virtuoso

Re: Finding Positon of characters

 
0
  #3
Dec 2nd, 2008
That's a better description of the project, but you could have posted it in your original thread.

As I see it the trick is to keep track of the original element indexes after you have sorted the array. I can think of two ways to do that.

First, copy the original array, sort the copied array, then loop through the original array to find the index of the element with the same value as the current element in the sorted array and place it in a third array containing the indexes in the original . Sort of a mapping of values in the sorted array to indexes in the original array.

Second option, create a struct holding the value and the index of each element in the original array, make an array of struct and sort it based on value, then display it using the indexes stored in each struct object.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Finding Positon of characters

 
0
  #4
Dec 2nd, 2008
Here's a sample code..The rest is yours..
  1. void swap(int items[], int i1, int i2)
  2. (
  3. int t = 0;
  4. t=items[i1];
  5. items[i1]=items[i2];
  6. items[i2]=t;
  7. )
  8. int main()
  9. {
  10. int n=0,m=0;
  11. const int s=4;
  12. int num[s]={4,1,2,3};
  13. for(;n<s;n++)
  14. std::cout<<num[n]<<std::endl;
  15. for(n=s-1;n>0;n--)
  16. for(m=0;m<n;m++)
  17. if(num[m]>num[m+1])
  18. swap(num,m,m+1);
  19. num[s-1]=0;
  20. std::cout<<std::endl;
  21. for(n=0;n<s;n++)
  22. std::cout<<num[n]<<std::endl;
  23. return 0;
  24. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Finding Positon of characters

 
0
  #5
Dec 2nd, 2008
Here's a sample code..The rest is yours..
  1. void swap(int items[], int i1, int i2)
  2. (
  3. int t = 0;
  4. t=items[i1];
  5. items[i1]=items[i2];
  6. items[i2]=t;
  7. )
  8. int main()
  9. {
  10. int n=0,m=0;
  11. const int s=4;
  12. int num[s]={4,1,2,3};
  13. int old[s]={4,1,2,3};
  14.  
  15. for(;n<s;n++)
  16. std::cout<<num[n]<<std::endl;
  17. for(n=s-1;n>0;n--)
  18. for(m=0;m<n;m++)
  19. if(num[m]>num[m+1])
  20. swap(num,m,m+1);
  21.  
  22. std::cout<<std::endl;
  23. for(n=0;n<s;n++)
  24. std::cout<<num[n]<<std::endl;
  25.  
  26. //...A few aid
  27. for x=0 to s-1
  28. for y=0 to s-1
  29. if num[x] = old[y] then
  30. the old pos=y
  31.  
  32. return 0;
  33. //done
  34. }
Last edited by cikara21; Dec 2nd, 2008 at 3:24 pm. Reason: :)
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Finding Positon of characters

 
0
  #6
Dec 2nd, 2008
By Just Skimming through your Problem statement one can judge its a sorting problem, what you need to do is to analyze which sort will meets your need, if you've small array and planty of space then the following trivial algorithm will meet your requirements. But this trivial algorithm only requires unique element.

Algorithm.
i) Find the MAX of Input Array in O(n) Time.
ii) Allocate the Large Enough Array and Initialize it with -1.
iii) Loop Through the Input Array and increment the counter of output array positions which equates to input_array value.
iv) Print the Ouput Array (where value is not negative).

again this is a tentative solution, you can think of another, I'll prefer if you check the Thomas H Corman Sorting Techniques, which suites your requirement, if you really want to learn the sorting techniques & mathematically inclined you should check "The Art of Computer Programming" By Knuth.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC