SillyWolfy

Дин. массив рабочий

Sep 28th, 2022 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int normpow(int x, int y)
  4. {
  5. int result;
  6. result = 1;
  7. while (y > 0)
  8. {
  9. result = result * x;
  10. y--;
  11. }
  12. return result;
  13. }
  14. void bSort(int list[], int listLen)
  15. {
  16. for (int m = 0; m < listLen; m++)
  17. {
  18. bool flag = true;
  19. for (int i = 0; i < listLen - 1; i++)
  20. {
  21. if (list[i] > list[i + 1])
  22. {
  23. swap(list[i], list[i + 1]);
  24. flag = false;
  25. }
  26. }
  27. if (flag == true)
  28. break;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. setlocale(LC_ALL, "rus");
  35. int num;
  36. cout << "Введите размер массива" << endl;
  37. cin >> num;
  38. int* a = new int[num];
  39. srand(time(0));
  40. for (int i = 0; i < num; i++)
  41. {
  42. a[i] = (rand() * normpow(-1, rand())) % 1000 + 1;
  43. cout << a[i] << " - " << i+1 << " элемент массива" << endl;
  44. }
  45. bSort(a, num);
  46. cout << "Отсортированный массив" << endl;
  47. for (int i = 0; i < num; i++)
  48. {
  49. cout << a[i] << endl;
  50. }
  51. cout << "5 наименьших значений массива: ";
  52. for (int i = 0; i < 5; i++)
  53. {
  54. if (i == 4)
  55. cout << a[i];
  56. else
  57. cout << a[i] << ", ";
  58. }
  59. delete[] a;
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment