Advertisement
enkov

Функция с параметри 2 масива - връща сумата от уникалните

Feb 8th, 2019
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. /*Да се напише функция, която получава параметри
  2. два масива от реални числа и връща сумата от елементите (числата),
  3. които се срещат само в единия от двата масива.*/
  4.  
  5.  
  6. #include "stdafx.h"
  7.  
  8. #include <iostream>
  9. #include <ctime>
  10. using namespace std;
  11.  
  12. double SumOfUniqueElements(double arr1[], const int size1,
  13.     double arr2[], const int size2)
  14. {
  15.     double sum = 0.0;
  16.     for (int i = 0; i < size1; i++)
  17.     {
  18.         bool f = true;
  19.         for (int j = 0; j < size2; j++)
  20.         {
  21.             if (arr1[i] == arr2[j])
  22.                 f = false;
  23.         }
  24.         if (f) sum = sum + arr1[i];
  25.     }
  26.     for (int i = 0; i < size2; i++)
  27.     {
  28.         bool f = true;
  29.         for (int j = 0; j < size1; j++)
  30.         {
  31.             if (arr2[i] == arr1[j])
  32.                 f = false;
  33.         }
  34.         if (f) sum = sum + arr2[i];
  35.     }
  36.     return sum;
  37. }
  38.  
  39. void PrintArray(double arr[], const int size, const char* S)
  40. {
  41.     cout << S;
  42.     for (int i = 0; i < size; ++i) cout << arr[i] << " ";
  43.     cout << endl;
  44. }
  45.  
  46. int main()
  47. {
  48.     srand((unsigned)time(NULL));
  49.     const int n1 = 10; const int n2 = 15;
  50.     double A[n1]; double B[n2];
  51.     // генерираме n числа 0..99
  52.     for (int i = 0; i<n1; ++i)
  53.         A[i] = (rand() % 100000) / (rand() % 100 + 1.0);
  54.     for (int i = 0; i<n2; ++i)
  55.         B[i] = (rand() % 100000) / (rand() % 100 + 1.0);
  56.     PrintArray(A, n1, "Array A now: ");
  57.     PrintArray(B, n2, "Array B now: ");
  58.     cout << "SumOfUniqueEl = " <<
  59.         SumOfUniqueElements(A, n1, B, n2) << endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement