Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2. int const MAX = 100;
  3.  
  4. void InitArray(int* a, int n) {
  5. for(int i = 0; i < n; i++) {
  6. std::cin >> a[i];
  7. }
  8. }
  9.  
  10. void BubbleSort(int* a, int n) {
  11. for (int i = n - 1; i > 0; i--) {
  12. for (int j = 0; j < i; j++) {
  13. if(a[j] > a[j+1]) {
  14. int tmp = a[j];
  15. a[j] = a[j+1];
  16. a[j+1] = tmp;
  17. }
  18. }
  19. }
  20. }
  21.  
  22. void FDupElemInArray(const int* a, int n) {
  23. int counter = 0;
  24. int reps = 0;
  25.  
  26. for(int i = 0; i < n; i++) {
  27. if(a[i] == a[i+1]) {
  28. counter++;
  29. reps++;
  30. continue;
  31. }
  32. if(reps != 0) {
  33. std::cout << a[i] << " : " << reps << std::endl;
  34. }
  35. reps = 0;
  36. }
  37. std::cout << "\nThe number of repetitive elements: " << counte$
  38. }
  39.  
  40. void ShowArray(const int* a, int n) {
  41. for(int i = 0; i < n; i++) {
  42. std::cout << a[i] << " ";
  43. }
  44. std::cout << "\n\n";
  45. }
  46.  
  47. int main() {
  48.  
  49. int arr[MAX] = {0};
  50.  
  51. InitArray(arr, MAX); // заполняем массив случайным$
  52. BubbleSort(arr, MAX); // сортируем массив
  53. ShowArray(arr, MAX); // выводим отсортированный массив
  54. FDupElemInArray(arr, MAX); // находим количество всех повторя$
  55. // и количество повторе$
  56. std::cin.get();
  57. std::cin.get();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement