Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array - finding nth highest term

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I need help writing a function that reads in a integer n from the user and displays the nth term in the array.

I have an array of size 26, Array[26].

0 is assigned letter 'a'
and 25 is assigned 'z' and so on for the rest of the letters

My program opens an infile and counts how many times each letter appeared.
So if it counts 67 letter 'a's then Array[0] = 67
" " "" " "" " ' " 89 letter 'k's then Array[10] = 89
etc for each letter.

I have that implemented but here is the problem.

The user inputs an integer into the function say 9. I want to output to the user the 9th highest occuring index.

So say the letter m, has the 9th highest value in the array. I want to output the index of m ie 13

The user inputs an integer into the function say 2. I want to output to the user the 2nd highest occuring index.

So say the letter d has the 2nd highest value in the array. I want to output the index of d ie 3

I have the rest of the program working well except for this part. Much help would he appreciated.
 
well, I would change the array a bit to use a struct that would hold the character and the count. Overload the ++ operator to increment the count so the code would not need much changing. Then, when your done, use the built in qsort method to sort the array in ascending or decending order. When the user types in 9 you can just do

cout<<&quot;The 9th higest count is: &quot;<<array[9-1].character<<endl;

Granted the 9-1 will be n-1 where n is the number the user entered.

Matt
 
create an array say OccuringSort[26]; work out an algorithm to fill the OccuringSort with the indexes of the Array in the order that the most occuring is the first and so on. Once you set up the array, it is easy to do the rest.

here is a sample of the sorting: (Bubble sorting)

int i, j;
for ( i=0; i < 26; i++) OccuringSort = i;

for ( int i=0; i <26; i++){
for (int j = i + 1 ; j<26;j++){
if (Array[OccuringSort[j]]>Array[OccuringSort[j -1]]){
int tmp = OccuringSort[j];
OccuringSort[j] = OccuringSort[j-1];
OccuringSort[j-1] = tmp;
}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top