Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int (*compfn)(const void*, const void*);
  5.  
  6. struct numbers { int number;
  7.  
  8. };
  9.  
  10. struct numbers array[10] = { { 1 },
  11. { 5 },
  12. { 8 },
  13. { 4 },
  14. { 3 },
  15. { 9 },
  16. { 2 },
  17. { 6 },
  18. { 10 },
  19. { 7 } };
  20.  
  21. void printarray(void);
  22. int compare(struct numbers *, struct numbers *);
  23.  
  24. void main(void)
  25. {
  26. char sign;
  27. while(1)
  28. {
  29. printf("Lista przed sortowaniem:\n");
  30. printarray();
  31.  
  32. qsort((void *) &array,
  33. 10,
  34. sizeof(struct numbers),
  35. (compfn)compare );
  36.  
  37. printf("\nLista po sortowaniu:\n");
  38. printarray();
  39. sign=scanf("%c",&sign);
  40. if(sign=='z')
  41. {
  42. break;
  43. }
  44. }
  45.  
  46. }
  47.  
  48. int compare(struct numbers *elem1, struct numbers *elem2)
  49. {
  50. if ( elem1->number < elem2->number)
  51. return -1;
  52.  
  53. else if (elem1->number > elem2->number)
  54. return 1;
  55.  
  56. else
  57. return 0;
  58. }
  59.  
  60. void printarray(void)
  61. {
  62. int i;
  63.  
  64. for (i = 0; i < 10; i++)
  65. printf("%d: Liczba %d to \n",
  66. i+1, array[i].number);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement