A
A
AlekseyPilatov2020-11-09 12:02:01
C++ / C#
AlekseyPilatov, 2020-11-09 12:02:01

How to calculate the geometric mean of the values ​​in 3 different arrays?

Develop a function that finds the geometric mean of all negative elements of arrays A(12), B(10), C(8), values ​​are set from the keyboard or randomly (it doesn't matter)

#include <iostream>
using namespace std;
int main()
{
    setlocale(LC_ALL, "Russian");
    int A = 12,
         B = 10,
         C = 8;
    int* arrA = new int[A];
    for (int i = 0; i < A; i++) {
        cin >> arrA[A];
    }
    cout << endl;
    int* arrB = new int[B];
    for (int i = 0; i < B; i++) {
        cin >> arrB[B];
    }
    cout << endl;
    int* arrC = new int[C];
    for (int i = 0; i < C; i++) {
        cin >> arrC[C];
    }
    cout << endl;

    return 0;
}


How, after entering the elements, calculate their geometric mean?
If I understand correctly, you need to put all these values ​​\u200b\u200bof 3 arrays into one common array and then calculate the geometric mean or sum it up at the end one at a time

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-11-09
@AlekseyPilatov

So I solved this task, everything works correctly (As far as I managed to check), I didn’t write very cleanly, so there are a lot of variables without which I think I could do without.
Here is the solution:

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  setlocale(LC_ALL, "rus");
  const int A = 12;
  const int B = 10; 
  const int C = 8;
  double ka = 0,kb=0,kc=0;
  double a1 = 1, a = 0, b = 0, c = 0, b1 = 1, c1 = 1;
  double crA = 0, crB = 0, crC=0,crABC=0;
  int* arrA = new int[A];
  int* arrB = new int[B];
  int* arrC = new int[C];
  cout << "Введите элементы массива A " << endl;
  for (int i = 0; i < A; i++) {
    cin >> arrA[i];
    if (arrA[i] < 0) {
      ++ka;
    }
  }
  
  for (int j = 0; j < A; j++) {
    if (0 > arrA[j]) {
      a =arrA[j];
      a1 *= a;
  
    }
  }
  ka = 1 / ka;
  crA = pow(a1, ka);

  cout << "Введите элементы массива Б " << endl;
  for (int i = 0; i < B; i++) {
    cin >> arrB[i];
    if (arrB[i] < 0) {
      ++kb;
    }
  }

  for (int j = 0; j < B; j++) {
    if (0 > arrB[j]) {
      b = arrB[j];
      b1 *= b;

    }
  }
  kb = 1 / kb;
  crB = pow(b1, kb);

  cout << "Введите элементы массива C " << endl;
  for (int i = 0; i < C; i++) {
    cin >> arrC[i];
    if (arrC[i] < 0) {
      ++kc;
    }
  }

  for (int j = 0; j < C; j++) {
    if (0 > arrC[j]) {
      c = arrC[j];
      c1 *= c;

    }
  }
  kc = 1 / kc;
  crC = pow(c1, kc);

  crABC = pow(crA * crB * crC,1.0/3.0);
  cout << "Среднее геометрическое всех отрицательных элементов в этих массивах = " << crABC << endl;

}

R
Ronald McDonald, 2020-11-09
@Zoominger

You have already asked.

If I understand correctly, you need to put all these values ​​\u200b\u200bof 3 arrays into one common array and then calculate the geometric mean or sum it up at the end one at a time

You can calculate it for each array and then find the arithmetic mean of the resulting three numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question