| | |
Finding Positon of characters
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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
Here's a sample code..The rest is yours..
C++ Syntax (Toggle Plain Text)
void swap(int items[], int i1, int i2) ( int t = 0; t=items[i1]; items[i1]=items[i2]; items[i2]=t; ) int main() { int n=0,m=0; const int s=4; int num[s]={4,1,2,3}; for(;n<s;n++) std::cout<<num[n]<<std::endl; for(n=s-1;n>0;n--) for(m=0;m<n;m++) if(num[m]>num[m+1]) swap(num,m,m+1); num[s-1]=0; std::cout<<std::endl; for(n=0;n<s;n++) std::cout<<num[n]<<std::endl; return 0; }
Here's a sample code..The rest is yours..
C++ Syntax (Toggle Plain Text)
void swap(int items[], int i1, int i2) ( int t = 0; t=items[i1]; items[i1]=items[i2]; items[i2]=t; ) int main() { int n=0,m=0; const int s=4; int num[s]={4,1,2,3}; int old[s]={4,1,2,3}; for(;n<s;n++) std::cout<<num[n]<<std::endl; for(n=s-1;n>0;n--) for(m=0;m<n;m++) if(num[m]>num[m+1]) swap(num,m,m+1); std::cout<<std::endl; for(n=0;n<s;n++) std::cout<<num[n]<<std::endl; //...A few aid for x=0 to s-1 for y=0 to s-1 if num[x] = old[y] then the old pos=y return 0; //done }
Last edited by cikara21; Dec 2nd, 2008 at 3:24 pm. Reason: :)
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Easy C++ Contest!
- Next Thread: Using string as char array
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






