Advertisement
CosmicFox33

3.15

Mar 2nd, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class arr
  6. {
  7. private:
  8. int* a;
  9. int len;
  10. public:
  11. arr(int Len) { a = new int[Len], len = Len; }
  12. arr(int Len, int x)
  13. {
  14. a = new int[Len], len = Len;
  15. for (int i = 0; i < Len; i++) a[i] = x;
  16. }
  17. ~arr() { delete[] a; cout << "Array was deleted.\n"; }
  18. void input() { for (int i = 0; i < len; i++) cin >> a[i]; }
  19. void output() { for (int i = 0; i < len; i++) cout << a[i] << " "; cout << endl; }
  20. void e_o(int* e, int* o)
  21. {
  22. int l, r;
  23. *e = 0; *o = 0;
  24. do {
  25. cin >> r >> l;
  26. if (l % 2 != 0 || r % 2 != 0) cout << "Indexes must be positive and not go beyond the array. Try again.\n";
  27. } while (l % 2 != 0 || r % 2 != 0 || r >= len || l >= len || r < 0 || l<0);
  28. if (l > r) swap(l, r);
  29. for (int i = l; i <= r; i++)
  30. if (a[i] % 2) *o += 1;
  31. else *e += 1;
  32. }
  33.  
  34.  
  35. };
  36.  
  37. int main(){
  38. int even, odd;
  39. arr A(7), B(5, 3);
  40. B.output();
  41. A.input();
  42. A.e_o(&even, &odd);
  43. cout << odd << " odd numbers and " << even << " even numbers are located between these indexes in the array.\n";
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement