Advertisement
Vladislav_Bezruk

Untitled

Oct 7th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int* readArray(int &n);
  6.  
  7. void calc(int *arr, int n, int &sum, int &count, int A);
  8.  
  9. int main() {
  10.    
  11.     int n, A, count, sum;
  12.     int* arr = readArray(n);
  13.    
  14.     cout << endl << "Enter A: ";
  15.     cin >> A;
  16.    
  17.     cout << endl << "===RESULT===" << endl << endl;
  18.    
  19.     calc(arr, n, sum, count, A);
  20.    
  21.     if (count) {
  22.         cout << "There are " << count << " elements which are divided into " << A << endl;
  23.         cout << "Sum = " << sum << endl;
  24.     } else {
  25.             cout << "There are no elements which are divided into " << A << endl;
  26.     }
  27.    
  28.     delete[] arr;
  29.    
  30.     return 0;
  31. }
  32.  
  33. int* readArray(int &n) {
  34.     cout << "Enter size of array: ";
  35.     cin >> n;
  36.    
  37.     int* arr = new int[n];
  38.    
  39.     cout << "Enter " << n << " elems of array: ";
  40.    
  41.     for (int i = 0; i < n; i++) {
  42.         cin >> arr[i];
  43.     }
  44.    
  45.     return arr;
  46. }
  47.  
  48. void calc(int *arr, int n, int &sum, int &count, int A) {
  49.     sum = count = 0;
  50.    
  51.     for (int i = 0; i < n; i++) {
  52.         if (arr[i] % A == 0) {
  53.             sum += arr[i];
  54.             count++;
  55.         }
  56.     }
  57.     return;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement