Нужно разбить код на функции
----------------------------------
#include
#include
#include
int main()
{
int VvodMass;
setlocale(LC_ALL, "rus");
std::cout << "Введите размер массива: ";
std::cin >> VvodMass; //ввод размерыности массива
int* Array = new int[VvodMass]; //выделение памяти
int Summa = 0;
std::cout << "Массив: " << std::endl; //вывод массива
for (int i = 0; i < VvodMass; i++)
{
std::cout << (Array[i] = rand() % 9 + 1) << " "; //заполнение рандомными цифрами
Summa += Array[i];
}
std::cout << "\n Сумма:" << Summa ; //вывод суммы
delete[] Array; //освобождение памяти
return 0;
}
C/C++
Помогите с кодом на С++
#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>
using namespace std;
size_t length(const char* msg) {
auto value = 0U;
while (!value) {
cout << msg;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
}
return value;
}
int* create(const size_t n) {
return new(nothrow) int[n];
}
void fill(int* box, const size_t n, int a, int b) {
if (a > b) swap(a, b);
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) box[i] = uid(gen);
}
void show(const int* box, const size_t n, const streamsize w) {
for (auto i = 0U; i < n; ++i) cout << setw(w) << box[i];
puts("");
}
long long sum(const int* box, const size_t n) {
auto s = 0LL;
for (auto i = 0U; i < n; ++i) s += box[i];
return s;
}
void destroy(int* box) {
if (box != nullptr) {
delete[] box;
box = nullptr;
}
}
int main() {
system("chcp 1251 > nul");
const auto n = length("Введите размер массива: ");
const auto box = create(n);
if (box != nullptr) {
fill(box, n, 1, 9);
show(box, n, 3U);
const auto s = sum(box, n);
cout << "Сумма: " << s << '\n';
destroy(box);
}
system("pause > nul");
}
#include <iomanip>
#include <random>
#include <cstdlib>
using namespace std;
size_t length(const char* msg) {
auto value = 0U;
while (!value) {
cout << msg;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
}
return value;
}
int* create(const size_t n) {
return new(nothrow) int[n];
}
void fill(int* box, const size_t n, int a, int b) {
if (a > b) swap(a, b);
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) box[i] = uid(gen);
}
void show(const int* box, const size_t n, const streamsize w) {
for (auto i = 0U; i < n; ++i) cout << setw(w) << box[i];
puts("");
}
long long sum(const int* box, const size_t n) {
auto s = 0LL;
for (auto i = 0U; i < n; ++i) s += box[i];
return s;
}
void destroy(int* box) {
if (box != nullptr) {
delete[] box;
box = nullptr;
}
}
int main() {
system("chcp 1251 > nul");
const auto n = length("Введите размер массива: ");
const auto box = create(n);
if (box != nullptr) {
fill(box, n, 1, 9);
show(box, n, 3U);
const auto s = sum(box, n);
cout << "Сумма: " << s << '\n';
destroy(box);
}
system("pause > nul");
}
Ну а х#ле ты сидишь. Лопату в руки и иди разбивай