Ввести одномерный массив A из N элементов. Вычислить Srg– среднее геометрическое положительных элементов массива. Заменить на Srg первый элемент массива. Массив вывести до и после преобразования. С++
Var s: string; a, b: integer; begin readln(s); if s[1] = 'x' then begin a := StrToInt(s[3]); b := StrToInt(s[5]); if s[2] = '-' then a := -a; writeln(b - a); end else if s[3] = 'x' then begin a := StrToInt(s[1]); b := StrToInt(s[5]); if s[2] = '-' then begin a := -a; b := -b; end; writeln(b - a); end else if s[5] = 'x' then begin a := StrToInt(s[1]); b := StrToInt(s[3]); if s[2] = '-' then b := -b; writeln(a + b); end end.
учтите что никакой защиты от дурака или неверного ввода
Код программы:
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <windows.h>
#include<locale>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
double* A, Srg, proizv = 1;
int kol = 0, i, N;
cout << "Введите количество элементов" << endl;
cin >> N;
A = (double*)malloc(N * sizeof(double));
cout << "Введите массив: " << endl;
for (i = 0; i < N; i++)
cin >> A[i];
for (i = 0; i < N; i++)
if (A[i] > 0) {
proizv = proizv * A[i];
++kol;
}
Srg = pow(proizv, 1./kol);
cout << "Исходный массив: " << endl;
for (i = 0; i < N; i++)
cout<< A[i] << ' ';
cout << '\n';
cout << "Измененный массив: " << endl;
A[0] = Srg;
for (i = 0; i < N; i++)
cout << A[i] << ' ';
return 0;
}