943,641 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 387
  • C++ RSS
Dec 2nd, 2008
0

Finding Positon of characters

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Awais Ahmad is offline Offline
5 posts
since Nov 2007
Dec 2nd, 2008
0

Re: Finding Positon of characters

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.
Reputation Points: 12
Solved Threads: 0
Light Poster
Ψmon is offline Offline
30 posts
since Mar 2007
Dec 2nd, 2008
0

Re: Finding Positon of characters

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 2nd, 2008
0

Re: Finding Positon of characters

Here's a sample code..The rest is yours..
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 2nd, 2008
0

Re: Finding Positon of characters

Here's a sample code..The rest is yours..
C++ Syntax (Toggle Plain Text)
  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: :)
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 2nd, 2008
0

Re: Finding Positon of characters

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.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Easy C++ Contest!
Next Thread in C++ Forum Timeline: Using string as char array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC