fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4.  
  5. void TreeRebuild(int r, int q)
  6. {
  7. int v;
  8. int i,j;
  9. int pp;
  10.  
  11. i=r; /*индекс рассматриваемого элемента*/
  12. v=mas[i]; /*рассматриваемый элемент*/
  13. j=2*i+1;/*индекс элемента, с которым проводится сравнение*/
  14. pp=0; /*предположение, что не найдено место в пирамиде*/
  15. while (j<=q && !pp)
  16. {
  17. if (j<q)
  18. if (mas[j]>mas[j+1]) /*сравнение с меньшим элементом*/
  19. j++;
  20. if (v<=mas[j])
  21. pp=1; /*элемент стоит на своем месте*/
  22. else
  23. {
  24. mas[i]=mas[j]; /*перестановка элемента*/
  25. i=j;
  26. j=2*i+1; /*прохождение дальше по пирамиде*/
  27. }
  28. }
  29. mas[i]=v;
  30. }
  31. void TreeSort(int mas[], int n)
  32. {
  33. int k,i;
  34. k=n/2; /*эта часть массива является пирамидой*/
  35. for (i=k-1; i>=0; i--)
  36. TreeRebuild(i, n-1);
  37. for (i=n-1; i>=1; i--)
  38. {
  39. swap(&mas[0],&mas[i]); /*перестановка 0-го и i-го элементов*/
  40. TreeRebuild(0,i-1); /* «проталкивание» i-го элемента*/
  41. }
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47. setlocale(LC_ALL,"");
  48. int a, b, n;
  49. int mas[1000];
  50. printf ("Введите диапазон значений массива: ");
  51. scanf("%d %d", &a, &b);
  52. printf("Введите количество элементов массива: ");
  53. scanf("%d", &n);
  54. elements (mas, a, b, n);
  55. display(mas, n);
  56. TreeSort(mas, n);
  57. display(mas, n);
  58. return 0;
  59. }
  60.  
  61. void elements (int mas[], int a, int b, int n)
  62. {
  63. int i;
  64. srand(time(NULL));
  65. for (i=0; i<n; i++)
  66. {
  67. mas[i] = rand() % (b - a + 1) + a;
  68. }
  69. }
  70. void display(int mas[], int n)
  71. {
  72. int i = 0;
  73. do
  74. {
  75. printf("%2d ", mas[i]);
  76. i++;
  77. } while (i < n);
  78. }
  79.  
  80.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘TreeRebuild’:
prog.c:12:7: error: ‘mas’ undeclared (first use in this function)
     v=mas[i]; /*рассматриваемый элемент*/
       ^~~
prog.c:12:7: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function ‘TreeSort’:
prog.c:39:9: warning: implicit declaration of function ‘swap’ [-Wimplicit-function-declaration]
         swap(&mas[0],&mas[i]); /*перестановка 0-го и i-го элементов*/
         ^~~~
prog.c: In function ‘main’:
prog.c:54:5: warning: implicit declaration of function ‘elements’ [-Wimplicit-function-declaration]
     elements (mas, a, b, n);
     ^~~~~~~~
prog.c:55:5: warning: implicit declaration of function ‘display’ [-Wimplicit-function-declaration]
     display(mas, n);
     ^~~~~~~
prog.c: At top level:
prog.c:61:6: warning: conflicting types for ‘elements’
 void elements (int mas[], int a, int b, int n)
      ^~~~~~~~
prog.c:54:5: note: previous implicit declaration of ‘elements’ was here
     elements (mas, a, b, n);
     ^~~~~~~~
prog.c: In function ‘elements’:
prog.c:64:11: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
     srand(time(NULL));
           ^~~~
prog.c: At top level:
prog.c:70:6: warning: conflicting types for ‘display’
 void display(int mas[], int n)
      ^~~~~~~
prog.c:55:5: note: previous implicit declaration of ‘display’ was here
     display(mas, n);
     ^~~~~~~
stdout
Standard output is empty