fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. void choicesSort(int*, const int);
  6. void Init_Array(const int, int*, const int, const int);
  7. void Print_Array(const int, const int*);
  8. void Work(int*, int);
  9.  
  10. int main()
  11. {
  12. const int max_size = 30, left = -15, right = 20;
  13. int my_array[max_size];
  14. Init_Array(max_size, my_array, left, right);
  15. printf("Вхідний масив:\n");
  16. Print_Array(max_size, my_array);
  17. printf("\n");
  18. choicesSort(my_array, max_size);
  19. printf("Вихідний масив:\n");
  20. Print_Array(max_size, my_array);
  21. printf("\n");
  22. Work(my_array, max_size);
  23. }
  24.  
  25. void Init_Array(const int n, int *mas, const int a, const int b)
  26. {
  27. srand(time(0));
  28. for (int i = 0;i<n; i++)
  29.  
  30. mas[i] = a + rand() % (b - a+1 );
  31. }
  32. void Print_Array(const int n, const int *mas)
  33. {
  34. for (int i = 0; i < n; i++)
  35.  
  36. printf("%3d", mas[i]);
  37. }
  38. void choicesSort(int *mas, const int n)
  39. {
  40. int min = 0;
  41. int buf = 0;
  42. for (int i = 0; i < n; i++)
  43. {
  44.  
  45. min = i;
  46. for (int j = i + 1; j < n; j++)
  47. min = (mas[j] < mas[min]) ? j : min;
  48. if (i != min)
  49. {
  50. buf = mas[i];
  51. mas[i] = mas[min];
  52. mas[min] = buf;
  53. }
  54. }
  55. }
  56. void Work(int *mas, int n)
  57. {
  58. int i;
  59. int alph = 0;
  60. float persent;
  61. for (int i = 0; i < n; i++)
  62. {
  63. if (mas[i] > 0)
  64. alph++;
  65. }
  66. persent = (alph / 30.) * 100.;
  67. printf("Відсоток чисел які мають додатнє значення: %5.2f %%\n", persent);
  68. }
Success #stdin #stdout 0s 15232KB
stdin
-3 5 2 11 19 14 10 9
stdout
Вхідний масив:
 -1 -3  4  5  1 -2 18  9 -1-14  7 10  1 16 -2-11 19  3 20 -7 -4  5 10 -6 -3 20 13  4 -7  1
Вихідний масив:
-14-11 -7 -7 -6 -4 -3 -3 -2 -2 -1 -1  1  1  1  3  4  4  5  5  7  9 10 10 13 16 18 19 20 20
Відсоток чисел які мають додатнє значення: 60.00 %