C/C++

Написать код на языке C++

Есть массив, Задать самому 10 элементов
спросить у пользователя число
прибавить к каждому элементу массива это число
вывести результат в виде массива
и потом сложить все элементы массива вывести сумму
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void Error()
{
printf("\nError. Array not created.");
exit(1);
}

void PrintArray(int *a, int size)
{
for(int i = 0; i < size; i++)
printf(" %d", a[i]);

}

int main()
{
int n=0, num=0, sum=0, *a;
printf("Enter size of array a[n], n = ");
scanf("%d", &n);
a=(int*)malloc(n*sizeof(int));
if(!a) Error();
srand(time(NULL));
for(int i = 0; i < n; i++)
a[i] = rand()%25;
printf("\nStart array:\n");
PrintArray(a, n);
printf("\n\nEnter your number, num = ");
scanf("%d", &num);
for(int i = 0; i < n; i++)
a[i] += num;
printf("\nResult array:\n");
PrintArray(a, n);
for(int i = 0; i < n; i++)
sum += a[i];
printf("\n\nSum of array is %d", sum);
free(a);
system("pause");
return 0;
}
КБ
Коля Болодурин
37 945
Лучший ответ
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 50);
mt19937 gen{ random_device()() };
const streamsize w = 3;
int box[10];
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << ' ' << setw(w) << x;
puts("");
cout << "Number: ";
int number;
cin >> number;
for (auto& x : box) x += number;
for (auto x : box) cout << ' ' << setw(w) << x;
puts("");
auto sum = 0LL;
for (auto x : box) sum += x;
cout << "Sum: " << sum << '\n';
}