Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include "head1.h"
  6. #define NMAX 20
  7.  
  8.  
  9. int read_array(const char* name, double *a, int maxn)
  10. {
  11.  
  12. int i;
  13. FILE* fp;
  14. if(!(fp = (fopen(name,"r"))))return -1;
  15. for(i = 0; i < maxn; i++)
  16. {
  17. if(fscanf(fp, "%lf", a + i)!= 1)
  18. {
  19. if(feof(fp))
  20. {
  21. fclose(fp);
  22. return -2;
  23. }
  24. else{
  25. fclose(fp); return i;
  26. }
  27. }
  28. }
  29. fclose(fp);
  30. return i;
  31. }
  32. void init_array(double *a, int n, int key)
  33. {
  34. int i;
  35. srand(key);
  36.  
  37. for(i = 0; i < n; i++)
  38. a[i] = rand();
  39. }
  40. void print_array(double *a, int n)
  41. {
  42. int i;
  43. int m = (n > NMAX? NMAX:n);
  44. for(i = 0; i < m; i++)
  45. printf("%lf ", a[i]);
  46.  
  47. }
  48.  
  49. void merge(double *a, double *c, int left, int right)
  50. {
  51. int i = 0;
  52. int j = 0;
  53. int middle = (left + right) / 2;
  54. c = a;
  55. while((i + j) < (right - left)){
  56. if(a[left + i] <= a[middle + j]){
  57. c[left + i + j] = a[left + i];
  58. if(i < (middle - left)){i++;}
  59. }else{
  60. c[left + j + i] = a[middle + j];
  61. if(j < (right - middle)){j++;}
  62. }
  63. }
  64. a = c;
  65. }
  66.  
  67. void sort(double *a, double* c, int n)
  68. {
  69. int k = 2;
  70. int j;
  71. while((1 >> k) < n)
  72. {
  73.  
  74. for(j = 0; j < (n / k); j++)
  75. {
  76. merge(a, c, k * j, k * (j + 1));
  77. }
  78. if (((n / k) * k) != n)
  79. {
  80. merge(a, c, k * (j + 1), n - 1);
  81. }
  82. k++;
  83. }
  84. }
  85.  
  86.  
  87.  
  88. int main(void)
  89. {
  90. int n, key;
  91. double *a, t, *c;
  92. int res;
  93. const char* name = "A.txt";
  94. printf("Input n and key\n");
  95. if(scanf("%d%d", &n, &key)!= 2)
  96. {
  97. printf("Cannot read");
  98. return 1;
  99. }
  100. if(!(a = (double*)malloc(n*sizeof(double))))
  101. {
  102. printf("Not enough memory!\n");
  103. return 2;
  104. }
  105. if (key == 0)
  106. {
  107. res = read_array(name, a, n);
  108. if (res < 0)
  109. {
  110. switch (res)
  111. {
  112. case (-1):
  113. {
  114. printf("Cannot open %s\n", name);
  115. break;
  116. }
  117. case (-2):
  118. {
  119. printf("Cannot read %s\n", name);
  120. break;
  121. }
  122. default:
  123. {
  124. printf("Unknown error %d in %s\n", res,name);
  125. }
  126. }
  127. free(a); return 3;
  128. }
  129.  
  130.  
  131. if (res == 0)
  132. {
  133. printf("The file %s is empty\n", name);
  134. return 0;
  135. }
  136. else n = res;}
  137. else init_array(a, n, key);
  138. c = (double*)malloc(n*sizeof(double));
  139. sort(a, c, n);
  140. print_array(a, n);
  141. free(a);
  142. free(c);
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement