C programming, write a program that arrange them into alphabetical order by usin
ID: 3816097 • Letter: C
Question
C programming, write a program that arrange them into alphabetical order by using struct form.
And can you teach me what the number in [ ] means ?
Here is the things to arrange into alphabetical order
const struct entry dictionary[500] =
{
{"zincic", "Pertaining to, containing, or resembling, zinc; zincous."},
{"zonulet", "A zonule."},
{"zymophyte", "A bacteroid ferment."},
{"zygenid", "Any one of numerous species of moths of the family Zygaenidae, most of which are bright colored. The wood nymph and the vine forester are examples. Also used adjectively."},
{"zumological", "Alt. of Zumometer"},
{"z", "Z, the twenty-sixth and last letter of the English alphabet"},
{"zoophorous", "The part between the architrave and cornice; the frieze; -- so called from the figures of animals carved upon it."},
{"zoisite", "A grayish or whitish mineral occurring in orthorhombic, prismatic crystals, also in columnar masses. It is a silicate of alumina and lime, and is allied to epidote."},
{"zincide", "A binary compound of zinc."},
{"zenith", "That point in:%s/^ the visible celestial hemisphere which is vertical to the spectator; the point of the heavens directly overhead; -- opposed to nadir."},
{"zygapophysis", "One of the articular processes of a vertebra, of which there are usually four, two anterior and two posterior. See under Vertebra."},
{"zoomorphic", "Of or pertaining to zoomorphism."},
{"zamite", "A fossil cycad of the genus Zamia."},
{"zodiacal", "Of or pertaining to the zodiac; situated within the zodiac; as, the zodiacal planets."},
{"zittern", "See Cittern."},
};
Explanation / Answer
The number in [ ] specifies the size of the arrray so although only 15 entries are allocated. Space of 500 entries have been provided to the dictionary. The rest of them are simply null at the time of initialisation.
#include <stdio.h>
#define max_size 1000
// A struct defines a type . So entry is a type much like int,char which contains two strings first and second
struct entry
{
char *first;
char *second;
} ;
// We need to tell the compiler how to compare two entries this function does that
int compare (const void * a, const void * b)
{
struct entry *A = (struct entry *)a; // This is type casting we need this to convert our inputs to this function entry
struct entry *B = (struct entry *)b;
int res = strcmp(A->first,B->first);
// strcmp compares two strings and returns -1,0,1 depending up being less, equal or more lexically
return res;
}
// This defines a array of entries
struct entry dictionary[500] =
{
{"zincic", "Pertaining to, containing, or resembling, zinc; zincous."},
{"zonulet", "A zonule."},
{"zymophyte", "A bacteroid ferment."},
{"zygenid", "Any one of numerous species of moths of the family Zygaenidae, most of which are bright colored. The wood nymph and the vine forester are examples. Also used adjectively."},
{"zumological", "Alt. of Zumometer"},
{"z", "Z, the twenty-sixth and last letter of the English alphabet"},
{"zoophorous", "The part between the architrave and cornice; the frieze; -- so called from the figures of animals carved upon it."},
{"zoisite", "A grayish or whitish mineral occurring in orthorhombic, prismatic crystals, also in columnar masses. It is a silicate of alumina and lime, and is allied to epidote."},
{"zincide", "A binary compound of zinc."},
{"zenith", "That point in:%s/^ the visible celestial hemisphere which is vertical to the spectator; the point of the heavens directly overhead; -- opposed to nadir."},
{"zygapophysis", "One of the articular processes of a vertebra, of which there are usually four, two anterior and two posterior. See under Vertebra."},
{"zoomorphic", "Of or pertaining to zoomorphism."},
{"zamite", "A fossil cycad of the genus Zamia."},
{"zodiacal", "Of or pertaining to the zodiac; situated within the zodiac; as, the zodiacal planets."},
{"zittern", "See Cittern."},
};
int main(int argc, char const *argv[])
{
qsort (dictionary, 15, sizeof(struct entry), compare); // Built in Function to sort we use
//15 to specify that there are only 15 valid entries to be sorted
int n;
for (n=0; n<15; n++)
printf ("%s %s ",dictionary[n].first,dictionary[n].second);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.