Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. const int MAX = 1024;
  5. void insert_sorted(long *sorted, int count, long value);
  6. /* inserts <value> into <sorted> containing <count> values */
  7.  
  8. void insert_sorted (long *sorted, int count, long value)
  9. {
  10. int i = 0;
  11.  
  12. sorted[count] = value;
  13. if (count == 0) return;
  14. for (i = count;i >= 0; i--)
  15. {
  16. if (value < sorted[i-1])
  17. sorted[i] = sorted[i-1];
  18. else break;
  19. }
  20. sorted[i] = value;
  21. }
  22.  
  23.  
  24. int main ()
  25. {
  26. FILE *infile = NULL;
  27. long sorted[1024];
  28. long value;
  29. int count = 0;
  30. int i = 0;
  31.  
  32. infile = fopen ("data.csv", "r");
  33.  
  34.  
  35.  
  36. if (NULL == infile)
  37. {
  38. perror ("cant open!!");
  39. return -1;
  40. }
  41.  
  42. /* while file not ends */
  43. while (!feof (infile))
  44. {
  45. fscanf (infile, "%ld\n", &value); /* fetch value */
  46. insert_sorted (sorted, count, value); /* sort */
  47. ++count; /* increase number of sorted values */
  48. }
  49.  
  50. /* display values */
  51. printf ("Sorted values : ");
  52. for (i = 0; i < count; i++ )
  53. {
  54. printf ("%ld ", sorted[i]);
  55. }
  56.  
  57. /*cleanup */
  58. if (infile)
  59. {
  60. fclose (infile);
  61. infile = NULL;
  62. }
  63.  
  64. return 0;
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement