1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <cmath>
#include <iostream>
#include <limits>
constexpr int ARR_SIZE = 10;
int* InArray(int* OneDimArray, size_t size);
int* OutArray(int* OneDimArray, size_t size);
void AvgStdDev(int* OneDimArray, size_t size);
void waitForEnter();
int main()
{
int OneDimArray[ARR_SIZE] {};
InArray(OneDimArray, ARR_SIZE);
waitForEnter();
return 0;
}
int* InArray(int* OneDimArray, size_t size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << "\nEnter " << size << " numbers [" << i+1 << "]: ";
std::cin >> OneDimArray[i];
std::cin.ignore(1);
}
OutArray(OneDimArray, size);
return OneDimArray;
}
int* OutArray(int* OneDimArray, size_t size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << "Array [" << i+1 << "]: " << OneDimArray[i] << '\n';
}
AvgStdDev(OneDimArray, size);
return OneDimArray;
}
void AvgStdDev(int* OneDimArray, size_t size)
{
int Sum = 0;
for (size_t i = 0; i < size; i++)
{
Sum += OneDimArray[i];
}
double Average = Sum / size;
double SoSD = 0;
for (size_t i = 0; i < size; i++)
{
SoSD += pow((OneDimArray[i] - Average), 2);
}
double StandDev = sqrt(SoSD / size);
double ValidRangesN = Average - (StandDev + 4);
double ValidRangesP = Average + StandDev + 4;
size_t Number = size;
for (size_t i = 0; i < size; i++)
{
if ((OneDimArray[i] < ValidRangesN) || (OneDimArray[i] > ValidRangesP))
{
std::cout << "This number will be dropped: " << OneDimArray[i] << '\n';
OneDimArray[i] = 0;
Number -= 1;
}
else
{
continue;
}
}
int NewSum = 0;
double NewAverage = 0.0,
NewSoSD = 0.0,
NewStandDev = 0.0;
if (Number < size)
{
for (size_t i = 0; i< size; i++)
{
NewSum = NewSum + OneDimArray[i];
}
NewAverage = NewSum / Number;
for (size_t i = 0; i < size; i++)
{
NewSoSD = NewSoSD + pow((OneDimArray[i] - NewAverage), 2);
}
NewStandDev = sqrt(NewSoSD / Number);
std::cout << "Average of " << size << " numbers: " << NewAverage << '\n';
std::cout << "Standard Deviation: " << NewStandDev << '\n';
}
else
{
std::cout << "Average of " << size << " numbers: " << Average << '\n';
std::cout << "Standard Deviation: " << StandDev << '\n';
}
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|