Решить в с++ задан массив z(n) целых чисел. найти максимальный элемент массива и если это простое число - удалить все элементы равные максимальному значению. определить среднее арифметическое положительных элементов массива после удаления. упорядочить массив по убыванию модулей.

kirillBruchov kirillBruchov    1   18.08.2019 08:00    0

Ответы
PesBarboss PesBarboss  23.04.2020 19:56
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef unsigned short int USI;

int* fillArray(string name, int s);
int maxArray(int[], int s);
bool isPrime(int);
void deleteAllEqualTo(int[], int s, int value);
float averageOfPositive(int[], int s);
void printArray(int[], int s);

bool sortByDescAbs(int i, int j) {
return abs(i) > abs(j);
}

int main() {
setlocale(LC_ALL, "Russian");
USI n;
cout << "n = ";
cin >> n;
int* z = fillArray("z", n);
int max = maxArray(z, n);
if ( isPrime(max) ) {
deleteAllEqualTo(z, n, max);
}
float avg = averageOfPositive(z, n);
cout << "среднее: " << avg << '\n';
sort(z, z + n, sortByDescAbs);
cout << "z[" << n << "]: ";
printArray(z, n);
return 0;
}

int* fillArray(string name, int s) {
int array[s];
for (int i = 0; i < s; i++) {
cout << name << "[" << i << "] = ";
cin >> array[i];
}
return array;
}

int maxArray(int a[], int s) {
int max = a[0];
for (int i = 1; i < s; i++) {
if (a[i] > max) max = a[i];
}
return max;
}

bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; (i*i) <= n; i += 2) {
if (n % i == 0 ) return false;
}
return true;
}

void deleteAllEqualTo(int a[], int s, int value) {
for (int i = 0; i < s; i++) {
if (a[i] == value) a[i] = 0;
}
}

float averageOfPositive(int a[], int s) {
unsigned int sum = 0,
count = 0;
for (int i = 0; i < s; i++) {
if (a[i] > 0) {
sum += a[i];
count++;
}
}
return (sum / count);
}

void printArray(int a[], int s) {
for (int i = 0; i < s; i++) {
cout << a[i] << ' ';
}
}
ПОКАЗАТЬ ОТВЕТЫ
Другие вопросы по теме Информатика