Ответ:
#include <iostream>
void addHeight(double& height, int& size, double*& arr)
{
if (arr == nullptr)
arr = new double[4];
arr[0] = height;
return;
}
if ((size + 1) % 4 == 0)
double* temp = new double[size + 1 + 4];
for (int i = 0; i < size; i++)
temp[i] = arr[i];
delete[] arr;
arr = temp;
arr[size] = height;
int main()
double* height = nullptr;
int size = 0;
while (true)
double elem = 0;
std::cout << "Enter height (END WITH '0'): ";
std::cin >> elem;
if (elem < 0)
std::cout << "Error: height should be more than 0\n";
continue;
else if (elem == 0)
break;
addHeight(elem, size, height);
size++;
double sum = 0;
sum += height[i];
std::cout.precision(2);
std::cout << std::fixed << "Overall height: " << sum << std::endl;
std::cout << std::fixed << "Average height: " << sum / size << std::endl;
delete[] height;
return 0;
Объяснение:
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
#include <iostream>
void addHeight(double& height, int& size, double*& arr)
{
if (arr == nullptr)
{
arr = new double[4];
arr[0] = height;
return;
}
if ((size + 1) % 4 == 0)
{
double* temp = new double[size + 1 + 4];
for (int i = 0; i < size; i++)
{
temp[i] = arr[i];
}
delete[] arr;
arr = temp;
}
arr[size] = height;
}
int main()
{
double* height = nullptr;
int size = 0;
while (true)
{
double elem = 0;
std::cout << "Enter height (END WITH '0'): ";
std::cin >> elem;
if (elem < 0)
{
std::cout << "Error: height should be more than 0\n";
continue;
}
else if (elem == 0)
{
break;
}
addHeight(elem, size, height);
size++;
}
double sum = 0;
for (int i = 0; i < size; i++)
{
sum += height[i];
}
std::cout.precision(2);
std::cout << std::fixed << "Overall height: " << sum << std::endl;
std::cout << std::fixed << "Average height: " << sum / size << std::endl;
delete[] height;
return 0;
}
Объяснение: