알파벳 순으로 정렬되도록 단어에 대한 포인터 배열을 정렬 할 수 있습니다. 문제는 정수 배열 (특정 단어가 사용 된 횟수)을 정렬해야 정수가 동일한 위치에 정렬된다는 것입니다. 각각의 단어로 :두 배열을 동시에 정렬하려면 qsort를 사용합니까?
내 코드 :
for (i = 0; i < numWords; i++) {
// prints out the words and their frequency respectively
printf("%s - %d\n", dictionary[i], frequency[i]);
}
//sorts the dictionary so that the words are 'alphabetical'
qsort(dictionary, numWords, sizeof(char *), rstrcmp);
printf("\nafter qsort\n"); //checkmark
for (i = 0; i < numWords; i++) {
// prints the word list alphabetically, but the frequencies are no longer matched
printf("%s - %d\n", dictionary[i], frequency[i]);
}
... 비교 기능 V
int rstrcmp(const void *p1, const void *p2) {
return strcmp(*(char * const *)p1, *(char * const *)p2);
}
이 이상적으로 당신이 해시 테이블 /지도를 사용할 수 있습니다 예를 들어
값과 그냥 키를 기반으로 정렬 .. – bwegs
할 간단한 일은 단어/주파수 쌍을 저장하고 다음 sor를 구조체를 사용하는 것입니다 이러한 구조체의 배열. – Turix
@Turix 나는 약간의 문제가 있을지 모르지만, 나는 포인터와 포인터를 잘 사용하지 않는다. 간신히 얻을, 구조체의 배열을 초기화하는 방법의 예를 보여주의합니까? – nobodyImportant