#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
void print_array(int* arr, int n) {
for (size_t i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int sum_in_vector(vector<int> vec) {
int sum = 0;
for (int elem : vec) {
sum += elem;
return sum;
void print_vector(vector<int> vec) {
cout << elem << " ";
int* auto_input_array(int* arr, int n) {
arr[i] = rand() % 200 - 100;
return arr;
vector<int> sum_of_the_first_four_positive_elements_in_the_array(int* arr, int n) {
vector<int> vec;
int count=0;
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
vec.push_back(arr[i]);
count++;
if (count == 4) {
return vec;
if (count < 4) {
cout << "Не удалось получить 4 положительных элемента!" << endl;
int main() {
srand(time(NULL));
setlocale(LC_ALL, "Russian");
int n;
cout << "Введите количество элементов в массиве: ";
cin >> n;
int* array = new int[n];
array = auto_input_array(array, n);
print_array(array, n);
vec = sum_of_the_first_four_positive_elements_in_the_array(array, n);
delete[] array;
print_vector(vec);
sum = sum_in_vector(vec);
cout << "Сумма равна: " << sum << endl;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
void print_array(int* arr, int n) {
for (size_t i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int sum_in_vector(vector<int> vec) {
int sum = 0;
for (int elem : vec) {
sum += elem;
}
return sum;
}
void print_vector(vector<int> vec) {
for (int elem : vec) {
cout << elem << " ";
}
cout << endl;
}
int* auto_input_array(int* arr, int n) {
for (size_t i = 0; i < n; i++) {
arr[i] = rand() % 200 - 100;
}
return arr;
}
vector<int> sum_of_the_first_four_positive_elements_in_the_array(int* arr, int n) {
vector<int> vec;
int count=0;
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
vec.push_back(arr[i]);
count++;
}
if (count == 4) {
return vec;
}
}
if (count < 4) {
cout << "Не удалось получить 4 положительных элемента!" << endl;
return vec;
}
}
int main() {
srand(time(NULL));
setlocale(LC_ALL, "Russian");
int n;
cout << "Введите количество элементов в массиве: ";
cin >> n;
int* array = new int[n];
vector<int> vec;
int sum = 0;
array = auto_input_array(array, n);
print_array(array, n);
vec = sum_of_the_first_four_positive_elements_in_the_array(array, n);
delete[] array;
print_vector(vec);
sum = sum_in_vector(vec);
cout << "Сумма равна: " << sum << endl;
}