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 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
What types are the arrays?
There's most likely another way.
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
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?
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
I would make a struct then.
Then you can just bubble sort based on the frequency without the relation losing meaning.Code:struct _alphabet { int frequency; char letter; };
I would just use
There is a slot for every ASCII character, so you can do:Code:int frequency[128] = { 0 };
frequency['A']++;
except 'A' could also be a variable.
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.
caN U show me an example I m stuck right now
What have you tried?
Instead of a hand out, what code do you have so far?
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 .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]]);
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.
Last edited by whiteflags; 11-05-2017 at 12:54 PM.
It only works because bubble sort is a stable sort.