# Thread: I need some c++ algorithm help

1. ## I need some c++ algorithm help

It's about 1:20am right now. I've been coding for the last 8 hours so my brain's a little fried. I need some help.
What i want to do seems like it should be fairly simple.
Lets say i have an array keyWord[4] = {m,i,k,e};
Since 'm' is the largest and 'e' is the smallest, the alphabets would be 'ranked' {3,1,2,0}.
I want an array to record these numbers such that keyWordOrder[4]={3,1,2,0}
So, basically, i want to be able to input any text (for simplicity, let's say 4 letters, no repeats) and have the keyWordOrder[] array contain the 'ranks'.
BTW, I don't want the original array keyWord[] to be sorted.
I'd really appreciate any help

2. Since I dont have much time (a busy morning) to get into depth on this.. Ill give the brief synopsis of what I'd probably do

Method 1: Use the ASCII values of each character to rank them...

Method 2: Have another array of all 26 letters (caps too, if you wanna get fancy), each with a rank... forinstance... ranks[26], and put a letter in each index 0-25.. and when the letters are put in your array, traverse the array and pstick the index value into the new array as it's rank. (THis assumes I understood what you wanted to do correctly)

I hope I made sense... It's early here and I'm on a caffiene deficiency...

[This message has been edited by jamis (edited 08-10-2001).]

3. jamis - I considered both methods you've suggested, before i posted. I don't think they'll work because i want the ranks to start at zero, no matter what word i input.
So 'john' would give {1,3,0,2}, 'prod' would give{2,3,1,0} etc.
I would use the ascii values to rank them, of course. But, how do i implement it without sorting the words or the rank?

[This message has been edited by moebius (edited 08-10-2001).]

[This message has been edited by moebius (edited 08-10-2001).]

4. Once you have an array with the ascii values stored (instead of the letters they represent), you could simply then replace the acsii values with your ranking system.. ie: the lowest number gets 0, the next, 1, the next 3 and so on.

For instance...

keyword[4] = {j,o,h,n};

would become (after the ascii tranformation, assuming all lowercase)

acsiiword[4] = {106,111,104,110};

You could then simply sort the numbers, which is quite an easy task... The acsii representations of the alphabet are in order... Then swap out the numbers with their new rank (0, 1, 2, 3, etc)

Lowercase:
a = 97
b = 98
...
y = 121
z = 122

Uppercase:
A = 65
B = 66
...
Y = 89
Z = 90

If you copy a char into an int, the int will become the acsii value of that char... ie:
Code:
```int num;
int letter = 'A';

/* the number 65 (acsii value of 'A' is stored in num */
num = letter;```

5. When you say..

"I would use the ascii values to rank them, of course. But, how do i implement it without sorting the words or the rank?"

Are you trying to say that you don't want the word coming out in alphabetical order, like, CAR would become ACR, or are you saying that for some reason, you don't want to use a sort algorithm? Because if you don't mind using a sort algorithm, I'd say go with jamis' idea. You would get the correct output that way.

-Jim

6. card_magic - you got it, i don't want the word to be sorted. 'john' must remain 'john' and not become 'hjno'. The ranks array of the characters in 'john' should be {1,3,0,2}. And these shouldn't be sorted either.
It's not that i don't want to use a sort algorithm, it's just that i don't want them sorted.
If i can get the ranks (1,3,0,2), i can take these numbers and use them in another 2X2 array, let's call array2. The first column in array2, [i][0], can be changed to [i][1] using the ranks. The second column in array2, [i][1], will become [i][3] and so on.
I'm not sure if i'm explaining this clear enough.
I really appreciate you guys trying. I really need this algo for my program to work.

7. i think i got it. i won't know for sure until i finish the rest of the function.

char keyWordOrder[4] = {0,0,0,0};
char keyWord{4] = {'j','o','h','n'};
for(i=0;i&lt;4;i++)
{
temp = keyWord[i];
for(j=0;j&lt;4;j++)
if(temp &gt; keyWord[i])
keyWordOrder[i] += 1;
}
Looks like i should end with {1,3,0,2} in keyWordOrder.
From analysing it on paper, it looks like it'll work. I'll know once the rest of the function's complete.

8. moebius,
I'm not sure that that will work right. I might be on crack, but I think that the way you have it set up, temp will always equal keyWord[i]. did you mean to write keyWord[j] after the second "for" statement? I think that the algo will work if you make that change....

however, I'm pretty rusty on how arrays work. it seems to me that you would want the keyWordOrder[] array to be an int array and not a char array if you're doing math on it like in the second "for" loop otherwise I'm not sure what your array will turn out like. If it's a char array, couldn't it possibly end out up like this

keyWordOrder[4] = {1,{1,1,1},0,{1,1}}

like I said tho... it's been a while since I've done work with arrays, and I only work in java, so what I'm saying might not be right.

of course.. then there's the whole problem of making this work for words that have different lengths. probably wouldn't be to hard to do by first testing the length of the input word, but.... just thinking...

-Z

9. Man!! That thing i posted is full of typos. Here's a copy and paste of what's already working:

for(i = 0; i &lt; 4; i++)
int keyWordOrder[4] = {0,0,0,0};
char keyWord[4] = {'j', 'o', 'h', 'n'};
char temp;
for(i = 0; i &lt; 4; i++)
{
temp = keyWord[i];
for(j = 0; j &lt; 4; j++)
if(temp &gt; keyWord[j])
keyWordOrder[i] += 1;
}

Thanx for pointing that out zskillz

p.s. The algorithm is copy and paste, not the initialization.

[This message has been edited by moebius (edited 08-10-2001).]

10. Hi Moebius,
The following code is a bit long winded and not very pretty, but it works. You get 3,1,2,0 in the variable 'keyWordOrder'
I hope this is what you are looking for, let us know.

int keyWordOrder[4] = {0,0,0,0};
char keyWord[4] = {'m', 'i', 'k', 'e'};

for(int x=0;x&lt;3;x++)
{
if(keyWord[0] &gt; keyWord[x+1])
keyWordOrder[0] += 1;
else
keyWordOrder[x+1] += 1;
}

for(int y=1;y&lt;3;y++)
{
if(keyWord[1] &gt; keyWord[y+1])
keyWordOrder[1] += 1;
else
keyWordOrder[y+1] += 1;
}

if(keyWord[2] &gt; keyWord[3])
keyWordOrder[2] += 1;
else
keyWordOrder[3] += 1;

for(int x=0;x&lt;4;x++)
cout &lt;&lt; keyWordOrder[x];

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q