# Thread: 2 Different types of array and I wan them to sort together.Do I need a struct?HELP

1. ## 2 Different types of array and I wan them to sort together.Do I need a struct?HELP

2 Different types of array and I wan them to sort together.Do I need a struct OR is an other way for doing it?HELP

2. What types are the arrays?

There's most likely another way.

3. Well the one is INT ARRAY and the other is a char array

Frequency of the character and the characters and i dont want to lose the connection of those two

4. In your first post you ask how you can sort them. In your last post you ask how you can count the frequency of the letters.

Does the int array contain integers that represent human readable chracters?

5. I have already done the frequency of the characters .Yes it contains integers and the other array is the alphabet.I just want to do a bubble sort to THE INT ARRAY and change the CHAR ARRAY(ALPHABET) accordingly

6. I would make a struct then.

Code:
```struct _alphabet
{
int frequency;
char letter;
};```
Then you can just bubble sort based on the frequency without the relation losing meaning.

7. I would just use
Code:
`int frequency[128] = { 0 };`
There is a slot for every ASCII character, so you can do:
frequency['A']++;
except 'A' could also be a variable.

8. Originally Posted by whiteflags
I would just use
Code:
`int frequency[128] = { 0 };`
There is a slot for every ASCII character, so you can do:
frequency['A']++;
except 'A' could also be a variable.
I was thinking he also wants to have the results sorted, which stops you from using the above method, doesn't it? Because if you sort the frequency of your example, the relation to letters would be lost.

9. That's a fair point. I guess it depends on what sorted means; if it should be ordered by increasing frequency, sorting the frequency array would lose the information. Otherwise, ASCII is sorted alphabetically.

10. caN U show me an example I m stuck right now

11. What have you tried?

Instead of a hand out, what code do you have so far?

12. Code:
``` //Frequency of the characters

int freq[26], holdposition[26];//array holding the frequency of the characters

for(i=0; i<26; i++)
{
freq[i] = 0;
holdposition[i] = 0;
}

char alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char str[WIDTH];
j=0;
int k;

while( j < numwords){
strcpy(str, wordpc[j]);
//printf("%s", str);
i=0;
while (str[i] != '\0'){
for( k=0; k<26; k++ ){
if (str[i] == alpha[k] ){
freq[k]++;
}
}
i++;
}
j++;
}

for (i = 0; i < 26; i++)
{
/** Printing only those characters
whose count is at least 1 */

if (freq[i] != 0)
printf("\n%c occurs %d times in the entered string.\n", alpha[i],freq[i]);
}

//Print out the character with the biggest frequency

/** Bubble Sort*/
int swap;
k=0;
for (i = 0 ; i < ( 26 - 1 ); i++)
{
for (j = 0 ; j < 26 - i - 1; j++)
{
if (freq[j] < freq[j+1]){
holdposition[k]= j;
k++;
swap = freq[j];
freq[j] = freq[j+1];
freq[j+1] = swap;
//holdposition related to the alphabet
}
else{
holdposition[k]= j;
k++;
}
}
}

for ( i = 0 ; i < 26 ; i++ )
printf("%d %c\n", freq[i], alpha[holdposition[i]]);```
So because unfortunately i havent study a lot of structures ,i couldnt do it so i tried this way which is tottaly wrong, anyway maybe you can show me some sites so i can do this .

13. One way to fix this would be, in the sort, where you swap one frequency element with another, to also swap one alpha element with another. Because they start off parallel to each other, if you always swap both arrays when you want to swap one, they will stay parallel. Also, you won't need holdposition[] if you do it this way.

14. Originally Posted by whiteflags
One way to fix this would be, in the sort, where you swap one frequency element with another, to also swap one alpha element with another. Because they start off parallel to each other, if you always swap both arrays when you want to swap one, they will stay parallel. Also, you won't need holdposition[] if you do it this way.
I don't know why this idea escaped me.

15. It only works because bubble sort is a stable sort.