Другие языки программирования и технологии
Привет всем! Помогите, пожалуйста, написать программу в C++.
Программа уже написана. Не мной. Преподаватель не поверил ) Нужно объяснять . Поясните, пожалуйста, что нужно делать. Алгоритм действий. Есть массив, в нем 12 элементов. Найти сумму всех положительных элементов, произведение отрицательных. Спасибочки.
Сдайте ему этот код и никаких вопросов к вам не возникнет.
#include <iostream>
#include <array>
#include <random>
#include <iomanip>
#include <string>
class array_12 {
public:
array_12();
array_12(int(&a)[12]);
const size_t size = 12;
typedef std::array<int, 12> array12;
typedef array12::const_iterator aci;
typedef array12::iterator ai;
void random_array(int = -10, int = 10);
long long sum_positive()const;
long long milt_negative()const;
void input_array();
bool is_positive()const;
bool is_negative()const;
private:
array12 v;
void reset();
friend std::ostream& operator<<(std::ostream&, const array_12&);
};
int randomize(int, int);
int input_int(const std::string = " enter one integer: ");
void without_positive();
void without_negative();
int main() {
array_12 a;
a.random_array();
std::cout << "\n random array: " << a << std::endl;
if (a.is_positive()) std::cout << " sum = " << a.sum_positive() << std::endl;
else without_positive();
if (a.is_negative()) std::cout << " mult = " << a.milt_negative() << std::endl;
else without_negative();
int b[12] = { -2, -3, -4, -5, -6, 3, 4, 5, 7, 8, 5, -2 };
array_12 c(b);
std::cout << "\n existing array: " << c << std::endl;
if (c.is_positive()) std::cout << " sum = " << c.sum_positive() << std::endl;
else without_positive();
if (c.is_negative()) std::cout << " mult = " << c.milt_negative() << std::endl;
else without_negative();
array_12 d;
std::cout << "\n\t enter the array:\n";
d.input_array();
std::cout << "\n entered array: " << d << std::endl;
if (d.is_positive()) std::cout << " sum = " << d.sum_positive() << std::endl;
else without_positive();
if (d.is_negative()) std::cout << " mult = " << d.milt_negative() << std::endl;
else without_negative();
std::cin.sync();
std::cin.get();
}
array_12::array_12() { reset(); }
array_12::array_12(int(&a)[12]) { for (auto i = 0u; i < size; ++i) v[i] = a[i]; }
void array_12::reset() { for (ai i = v.begin(); i != v.end(); ++i) *i = 0; }
void array_12::random_array(int l, int r) { for (ai i = v.begin(); i != v.end(); ++i) *i = randomize(l, r); }
std::ostream& operator<<(std::ostream& out, const array_12& arr) {
for (array_12::aci i = arr.v.begin(); i != arr.v.end(); ++i) out << ' ' << *i;
return out;
}
long long array_12::sum_positive()const {
long long sum = 0;
for (aci i = v.begin(); i != v.end(); ++i) if (*i > 0) sum += *i;
return sum;
}
long long array_12::milt_negative()const {
long long mult = 1;
for (aci i = v.begin(); i != v.end(); ++i) if (*i < 0) mult *= *i;
return mult;
}
void array_12::input_array() { for (ai i = v.begin(); i != v.end(); ++i) *i = input_int(); }
bool array_12::is_positive()const {
for (aci i = v.begin(); i != v.end(); ++i) if (*i >= 0) return true;
return false;
}
bool array_12::is_negative()const {
for (aci i = v.begin(); i != v.end(); ++i) if (*i < 0) return true;
return false;
}
int randomize(int left, int right) {
std::uniform_int_distribution<int> rand(left, right);
std::random_device rnd;
return rand(rnd);
}
int input_int(const std::string msg) {
int i;
do {
std::cout << msg;
std::cin >> i;
if (std::cin.good()) return i;
else {
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cout << "\aError!\n";
}
} while (true);
}
void without_positive() { std::cout << " positive numbers are missing" << std::endl; };
void without_negative() { std::cout << " negative numbers are missing" << std::endl; };
#include <iostream>
#include <array>
#include <random>
#include <iomanip>
#include <string>
class array_12 {
public:
array_12();
array_12(int(&a)[12]);
const size_t size = 12;
typedef std::array<int, 12> array12;
typedef array12::const_iterator aci;
typedef array12::iterator ai;
void random_array(int = -10, int = 10);
long long sum_positive()const;
long long milt_negative()const;
void input_array();
bool is_positive()const;
bool is_negative()const;
private:
array12 v;
void reset();
friend std::ostream& operator<<(std::ostream&, const array_12&);
};
int randomize(int, int);
int input_int(const std::string = " enter one integer: ");
void without_positive();
void without_negative();
int main() {
array_12 a;
a.random_array();
std::cout << "\n random array: " << a << std::endl;
if (a.is_positive()) std::cout << " sum = " << a.sum_positive() << std::endl;
else without_positive();
if (a.is_negative()) std::cout << " mult = " << a.milt_negative() << std::endl;
else without_negative();
int b[12] = { -2, -3, -4, -5, -6, 3, 4, 5, 7, 8, 5, -2 };
array_12 c(b);
std::cout << "\n existing array: " << c << std::endl;
if (c.is_positive()) std::cout << " sum = " << c.sum_positive() << std::endl;
else without_positive();
if (c.is_negative()) std::cout << " mult = " << c.milt_negative() << std::endl;
else without_negative();
array_12 d;
std::cout << "\n\t enter the array:\n";
d.input_array();
std::cout << "\n entered array: " << d << std::endl;
if (d.is_positive()) std::cout << " sum = " << d.sum_positive() << std::endl;
else without_positive();
if (d.is_negative()) std::cout << " mult = " << d.milt_negative() << std::endl;
else without_negative();
std::cin.sync();
std::cin.get();
}
array_12::array_12() { reset(); }
array_12::array_12(int(&a)[12]) { for (auto i = 0u; i < size; ++i) v[i] = a[i]; }
void array_12::reset() { for (ai i = v.begin(); i != v.end(); ++i) *i = 0; }
void array_12::random_array(int l, int r) { for (ai i = v.begin(); i != v.end(); ++i) *i = randomize(l, r); }
std::ostream& operator<<(std::ostream& out, const array_12& arr) {
for (array_12::aci i = arr.v.begin(); i != arr.v.end(); ++i) out << ' ' << *i;
return out;
}
long long array_12::sum_positive()const {
long long sum = 0;
for (aci i = v.begin(); i != v.end(); ++i) if (*i > 0) sum += *i;
return sum;
}
long long array_12::milt_negative()const {
long long mult = 1;
for (aci i = v.begin(); i != v.end(); ++i) if (*i < 0) mult *= *i;
return mult;
}
void array_12::input_array() { for (ai i = v.begin(); i != v.end(); ++i) *i = input_int(); }
bool array_12::is_positive()const {
for (aci i = v.begin(); i != v.end(); ++i) if (*i >= 0) return true;
return false;
}
bool array_12::is_negative()const {
for (aci i = v.begin(); i != v.end(); ++i) if (*i < 0) return true;
return false;
}
int randomize(int left, int right) {
std::uniform_int_distribution<int> rand(left, right);
std::random_device rnd;
return rand(rnd);
}
int input_int(const std::string msg) {
int i;
do {
std::cout << msg;
std::cin >> i;
if (std::cin.good()) return i;
else {
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cout << "\aError!\n";
}
} while (true);
}
void without_positive() { std::cout << " positive numbers are missing" << std::endl; };
void without_negative() { std::cout << " negative numbers are missing" << std::endl; };
препод -- молодец. у него было достаточно времени за вами понаблюдать и понять что из написанного ваше и что нет.
показывай код которые тебе написали.
а мы скажем где ты спалилась...
показывай код которые тебе написали.
а мы скажем где ты спалилась...
твой код? #define tipa int
#define blja 1
#define pokeda return
#define karoche for(
#define eta =
#define i +
#define da +=
#define uhty *=
#define lazha <=
#define chutok 15
#define poneslas main(
#define vaashe )
#define nah *
#define jopta []
#define chotko const
#define chiki char
#define nukablja {
#define joba }
#define ha ;
#define zaceni printf(
#define nu ,
tipa poneslas tipa hujeta nu chotko chiki nah voteta jopta vaashe nukablja
tipa sran eta blja ha
karoche tipa pizdec eta blja ha pizdec lazha chutok ha pizdec da blja vaashe nukablja
sran uhty pizdec ha
zaceni "Factorial: %d = %d\n" nu pizdec nu sran vaashe ha
joba
pokeda blja ha
joba
#define blja 1
#define pokeda return
#define karoche for(
#define eta =
#define i +
#define da +=
#define uhty *=
#define lazha <=
#define chutok 15
#define poneslas main(
#define vaashe )
#define nah *
#define jopta []
#define chotko const
#define chiki char
#define nukablja {
#define joba }
#define ha ;
#define zaceni printf(
#define nu ,
tipa poneslas tipa hujeta nu chotko chiki nah voteta jopta vaashe nukablja
tipa sran eta blja ha
karoche tipa pizdec eta blja ha pizdec lazha chutok ha pizdec da blja vaashe nukablja
sran uhty pizdec ha
zaceni "Factorial: %d = %d\n" nu pizdec nu sran vaashe ha
joba
pokeda blja ha
joba
ЛУЧШЕ КИНЬ КОД - А МЫ ЕГО РАЗКРИТИКУЕМ ТАК КАК ОН ТЫКНЕТ ПАЛЬЦЕМ В ЛЮБОЕ МЕСТО КОДА И СПРОСИТ ЭТО ЧТО ?
Ну дык возьми и сама напиши, тем более, что готовая уже есть.
проходишь по массиву проверяешь положительный ли элемент массива если положительный кладешь в сумму положительных элементов, иначе проверяешь если элемент отрицательный умножаешь на произведение отрицательных.
обьяснение типа того не знаю больше как можно сказать
обьяснение типа того не знаю больше как можно сказать
Похожие вопросы
- Помогите пожалуйста написать программу на c++.
- Помогите, пожалуйста, написать программу на C++.
- Помогите пожалуйста написать программу на C#
- Помогите пожалуйста написать программу на c++. Циклически сдвинуть массив на k элементов влево.
- Помогите пожалуйста написать программу на c++. Упорядочить элементы одномерного массива по убыванию.
- Помогите пожалуйста написать программу на c++, или хотя бы скиньте ссылку, если не сложно, где можно найти.
- Помогите пожалуйста написать программу в с++
- Народ, помогите пожалуйста с программой в C#
- Помогите, пожалуйста, написать программу на делфи с ассемблерными вставками.
- Помогите, пожалуйста, написать программу на С++