Sorting structures

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Sorting structures

 
0
  #1
Apr 3rd, 2006
How do I make selection sort work with strings in a structure?? My compiler doesn't seem to like my variable assignments under the show_alpha() function. :cry:



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SMAX 12
  6. #define LEN 20
  7. #define EMPTY 0
  8.  
  9. void show_empty_num(struct data *);
  10. void show_empty_list(struct data *);
  11. void show_alpha(struct data *, FILE *, int);
  12. void assign_seat(struct data *, FILE *, int);
  13. void del_seat(struct data *, FILE *, int);
  14. void quit();
  15. void prn_menu();
  16.  
  17. struct data {
  18. int seatid[1];
  19. int seatm[1];
  20. char first[LEN];
  21. char last[LEN];
  22. };
  23.  
  24. int main(void)
  25. {
  26. struct data flight[SMAX];
  27. FILE *fp;
  28. char input;
  29. int size = sizeof (struct data);
  30. int i = 0, cnt = 0;
  31.  
  32. while (i < SMAX)
  33. {
  34. flight[i].seatm[0] = 0;
  35. i++;
  36. }
  37.  
  38. if ((fp = fopen("file1.txt", "a+b")) == NULL)
  39. {
  40. puts("Error opening file");
  41. exit(0);
  42. }
  43.  
  44. rewind(fp);
  45.  
  46. while (cnt < SMAX && fread(&flight[cnt], size, 1, fp) == 1)
  47. cnt++;
  48.  
  49. prn_menu();
  50.  
  51. while ((input = getchar()) != EOF)
  52. {
  53. switch (input)
  54. {
  55. case 'a':
  56. show_empty_num(flight);
  57. prn_menu();
  58. break;
  59. case 'b':
  60. show_empty_list(flight);
  61. prn_menu();
  62. break;
  63. case 'c':
  64. show_alpha(flight, fp, size);
  65. prn_menu();
  66. break;
  67. case 'd':
  68. assign_seat(flight, fp, size);
  69. prn_menu();
  70. break;
  71. case 'e':
  72. del_seat(flight, fp, size);
  73. prn_menu();
  74. break;
  75. case 'f':
  76. fclose(fp);
  77. quit();
  78. break;
  79. }
  80. }
  81. return 0;
  82. }
  83.  
  84. void show_empty_num(struct data *flight)
  85. {
  86. int cnt = 0, i = 0;
  87. while (cnt < SMAX)
  88. {
  89. if (flight[cnt].seatm[0] == EMPTY)
  90. i++;
  91. cnt++;
  92. }
  93. printf("Number of empty seats = %d\n", i);
  94. }
  95.  
  96. void show_empty_list(struct data *flight)
  97. {
  98. int cnt = 0;
  99. while (cnt < SMAX)
  100. {
  101. if (flight[cnt].seatm[0] == EMPTY)
  102. printf("Seat %d is empty\n", cnt);
  103. cnt++;
  104. }
  105. }
  106.  
  107. void show_alpha(struct data *flight, FILE *fp, int size)
  108. {
  109. int i = 0, j = 0, cnt = 0, tmp = 0;
  110. char *temp;
  111.  
  112. for (i = 0; i < SMAX - 1; i++)
  113. {
  114. for (j = i + 1; j < SMAX; j++)
  115. if (strcmp(flight[i].first, flight[j].first) < 0)
  116. {
  117. temp = flight[i].first;
  118. flight[i].first = flight[j].first;
  119. flight[j].first = temp;
  120.  
  121. temp = flight[i].last;
  122. flight[i].last = flight[j].last;
  123. flight[j].last = temp;
  124.  
  125. tmp = flight[i].seatid[0];
  126. flight[i].seatid[0] = flight[j].seatid[0];
  127. flight[j].seatid[0] = tmp;
  128.  
  129. }
  130. }
  131.  
  132. while (cnt < SMAX)
  133. {
  134. printf("%s %s ID No: %d\n", flight[cnt].first, flight[cnt].last, flight[cnt].seatid[0]);
  135. cnt++;
  136. }
  137.  
  138. cnt = 0;
  139. while (cnt < SMAX)
  140. fread(&flight[cnt], size, 1, fp);
  141. }
  142.  
  143. void assign_seat(struct data *flight, FILE *fp, int size)
  144. {
  145. char ans;
  146. int num;
  147. puts("Enter the seat number. 99 to stop:");
  148.  
  149. while (scanf("%d", &num) == 1)
  150. {
  151. if (num == 99)
  152. break;
  153. if (num > SMAX || num < 0 || flight[num].seatm[0] == 1)
  154. {
  155. puts("Not a valid number or seat is occupied!");
  156. puts("Enter the seat number. 99 to stop:");
  157. continue;
  158. }
  159. else
  160. {
  161. flight[num].seatm[0] = 1;
  162. puts("Enter a seat ID:");
  163. scanf("%d", &flight[num].seatid[0]);
  164.  
  165. fflush(stdin);
  166.  
  167. puts("Enter a first name:");
  168. gets(flight[num].first);
  169. puts("Enter a last name:");
  170. gets(flight[num].last);
  171. fwrite(&flight[num], size, 1, fp);
  172. }
  173. puts("Would you like to assign another seat? (Y)es or (N)o");
  174. scanf("%c", &ans);
  175. if (ans == 'Y')
  176. {
  177. puts("Enter the seat number. 99 to stop:");
  178. continue;
  179. }
  180. else
  181. break;
  182. }
  183.  
  184. }
  185.  
  186. void del_seat(struct data *flight, FILE *fp, int size)
  187. {
  188. int num;
  189. char ans;
  190. puts("Enter a seat number 0-11. 99 to stop:");
  191.  
  192. while (scanf("%d", &num) == 1)
  193. {
  194. if (num == 99)
  195. break;
  196. if (num > SMAX || num < 0 || flight[num].seatm[0] == 0)
  197. {
  198. puts("Not a valid number or seat is empty!");
  199. puts("Enter a seat number beteen 0-11. 99 to stop:");
  200. continue;
  201. }
  202. else
  203. {
  204. flight[num].seatid[0] = EMPTY;
  205. flight[num].seatm[0] = EMPTY;
  206. flight[num].first[0] = '\0';
  207. flight[num].last[0] = '\0';
  208. fwrite(&flight[num], size, 1, fp);
  209. }
  210. fflush(stdin);
  211. puts("Would you like to delete another seat? (Y)es or (N)o");
  212. scanf("%c", &ans);
  213. if (ans == 'Y')
  214. {
  215. puts("Enter a seat number 0-11. 99 to stop:");
  216. continue;
  217. }
  218. else
  219. break;
  220. }
  221. }
  222.  
  223. void prn_menu()
  224. {
  225. puts("To choose a function, enter its label:");
  226. puts("a) Show number of empty seats");
  227. puts("b) Show list of empty seats");
  228. puts("c) Show alphabetical list of seat assignments");
  229. puts("d) Assign a customer to a seat");
  230. puts("e) Delete a seat assignment");
  231. puts("f) Quit");
  232. }
  233.  
  234. void quit()
  235. {
  236. puts("Have a nice day !");
  237. exit(0);
  238. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sorting structures

 
0
  #2
Apr 3rd, 2006
you are attempting to use c++ techniques on C strings. use strcpy() to copy one string to another
  1. char tmp[LEN];
  2. strcpy(tmp, flight[i].first);

However, that is not really what you want to do anyway -- it takes to much time to swap individual members of a structure. You want to swap the entire structure in one shot.
  1. struct data temp;
  2. ...
  3. ...
  4. // now swap the structures
  5. memcpy(&temp,&flight[i], sizeof(struct data));
  6. memcpy(&flight[i], &flight[j], sizeof(struct data));
  7. memcpy(&flight[j], &temp, sizeof(struct data));
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Sorting structures

 
0
  #3
Apr 4th, 2006
well nevermind i figured out the first part of the question that i had.

my next question has to do with changing the contents of the array of structures. How do I keep the original items from being altered?
Thanks in advance for any replies you can give.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sorting structures

 
0
  #4
Apr 4th, 2006
Originally Posted by degamer106
my next question has to do with changing the contents of the array of structures. How do I keep the original items from being altered?
make a duplicate copy of the array (or save it in a data file) before it is sorted.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Sorting structures

 
0
  #5
Apr 7th, 2006
Here, see this, maybe you'll find it useful
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_NUM 20
  5. #define NUM 5
  6.  
  7. /* It's sometimes clever to create an array of pointers to structures, because
  8. swapping structures may be very expensive since structures can be large.
  9. With array of pointers you actually sort pointers (4 bytes on mosth 32 bit machines)
  10. but not the actual data. This is just example, may be you'll find it useful. */
  11.  
  12. typedef struct Example
  13. {
  14. int data;
  15. char name[MAX_NUM];
  16. /* other data */
  17. }example;
  18.  
  19. void swap_pointers (example** pex1, example** pex2)
  20. {
  21. example* tmp;
  22.  
  23. tmp = *pex1;
  24. *pex1 = *pex2;
  25. *pex2 = tmp;
  26. }
  27.  
  28. int main (void)
  29. {
  30. example array_struct[NUM];
  31. example* array_pointers[NUM];/* for easier and more efficient sorting */
  32. int i, j;
  33. int min;
  34.  
  35. /* Just for example, I'll fill structures with string names */
  36. strcpy (array_struct[0].name, "Asim");
  37. strcpy (array_struct[1].name, "Asmir");
  38. strcpy (array_struct[2].name, "Emir");
  39. strcpy (array_struct[3].name, "Nermin");
  40. strcpy (array_struct[4].name, "Jasmin");
  41.  
  42. for (i = 0; i < NUM; i++)
  43. {
  44. array_pointers[i] = &array_struct[i];
  45. }
  46.  
  47. /* now, here goes implementation of selection sort algorithm on structures*/
  48.  
  49. for (i = 0; i < NUM; i++)
  50. {
  51. min = i;
  52. for (j = i + 1; j < NUM; j++)
  53. {
  54. if (strcmp (array_pointers[min]->name, array_pointers[j]->name) > 0)
  55. {
  56. min = j;
  57. }
  58. }
  59. swap_pointers (&array_pointers[i], &array_pointers[min]);
  60. }
  61. /* As you can see, you're not actually sorting the array of structures, but
  62. array of pointers to structures. Now you can display data in sort order or
  63. write them to file. */
  64. for (i = 0; i < NUM; i++)
  65. {
  66. printf("%s ", array_pointers[i]->name);
  67. }
  68.  
  69. return 0;
  70. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC