c - Can't sort array of structures alphabetically -


i trying sort strings in array of structures alphabetically print, code doesn't work.

i've been trying figure out why past few hours, can't figure out. i'm sure super obvious, i've been programming few weeks , can't figure out.

it compile no errors, output print of original unsorted array without aardvark, so: boy acumen addle cat affix agar ahoy aigrette ajar

here code far:

#include <stdio.h>  struct entry {     char    word[15];     char    definition[50]; }; struct entry dictionary[100] =    { {"boy",         "a boy          "                   },     {"aardvark",    "a burrowing african mammal"        },     {"acumen",      "mentally sharp; keen"              },     {"addle",       "to become confused"                },     {"cat",         "a cat"                             },     {"affix",       "to append; attach"                 },     {"agar",        "a jelly made seaweed"         },     {"ahoy",        "a nautical call of greeting"       },     {"aigrette",    "an ornamental cluster of feathers" },     {"ajar",        "partially opened"                  }    };    int main(void) {     int i;     void dictionarysort(struct entry dictionary[]);      dictionarysort(dictionary);      for(i = 0; < 10; ++i)     {         printf("%s\n", dictionary[i].word);     }      return 0; }  void dictionarysort(struct entry dictionary[]) {     int i, k, j;     struct entry temp[100];      for(i = 0; <=  9; ++i)     {         for( k = 0; dictionary[i].word[k] != '\0'; ++k)         {             if( (dictionary[i].word[k] > dictionary[i+1].word[k] ) )             {                 temp[i] = dictionary[i];                 dictionary[i] = dictionary[i+1];                 dictionary[i+1] = temp[i];             }          }      } } 

if has input appreciate it.

first of all, algorithm trying build not sorting. have here (after fixing issues described below) 1 iteration of bubble sort. make sort array need call dictioarysort 10 times. see https://en.wikipedia.org/wiki/bubble_sort more details.

now other issues in code. can simplify entire loop using strcmp:

for(i = 0; <=  9; ++i) {     if( strcmp(dictionary[i].word, dictionary[i+1].word ) > 0 )     {         temp[i] = dictionary[i];         dictionary[i] = dictionary[i+1];         dictionary[i+1] = temp[i];     } } 

but if making kind of exercise , want figure out how way, there 2 issues logic:

  1. consider words "azc" , "brc". in alphabetical order, don't need swapped. after @ first characters, a , b correspondingly, should stop comparing them. instead continue next letter, z , r correspondingly, , decide swap them based on that, leading incorrect order.

  2. after swap 2 words, should stop. consider case of za, rb. after looking @ first letters, z , r, swap words (which good). @ second letters. time words swapped, @ b , a, , swap them again. full solution along lines of:

for(i = 0; <=  9; ++i) {     for( k = 0; dictionary[i].word[k] != '\0'; ++k)     {         if( (dictionary[i].word[k] > dictionary[i+1].word[k] ) )         {             temp[i] = dictionary[i];             dictionary[i] = dictionary[i+1];             dictionary[i+1] = temp[i];             break; // <<-- new         }         else if( (dictionary[i].word[k] < dictionary[i+1].word[k] ) )         {             break; // <<-- new         }     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -