Вопрос помогите написать правильный код на с++ !

Регистрация
6 Июл 2013
Сообщения
83
Репутация
0
Спасибо
0
Монет
0
Дан массив из 10 целых элементов. Заполнить его случайными числами от 7 до 14 и вывести его на экран. Поменять все числа кратные 7 на 0. Вывести полученный массив на экран.
 
#include < iostream > #include < windows.h > #include < algorithm > #include < ctime > #include < cstdlib > using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); srand(time(NULL)); system("color 0A"); unsigned arr[10]; auto gen = []() { return 7 + rand() % (14 - 7 + 1); }; auto show = [](unsigned t) { cout << t << " "; }; auto editor = [](unsigned &t) { if (t % 7 == 0) { t = 0; } }; generate(arr, arr + 10, gen); cout << "Исходный массив: " << endl; for_each(arr, arr + 10, show); cout << endl; for_each(arr, arr + 10, editor); cout << "Преобразованный массив: " << endl; for_each(arr, arr + 10, show); cout << endl; system("pause"); return 0; }
 
18290142_e937739bd58f68dd5fb8ada4f309c38d_800.png
 
#include "iostream" #include "ctime" #include "cstdlib" using namespace std; int main(){ int a[10]; srand(time(NULL)); for(auto& i:a)i=rand()%8+7; for(auto& i:a)cout<< i<<; cout<< endl; for(auto& i:a)i*=!!(i%7); for(auto& i:a)cout<< i<<; cout<< endl; cin.get();}
 
Сколько программистов, столько и реализаций :^) Вот тебе реализация на современном С++ #include <iostream> #include <random> #include <array> #include <algorithm> template<size_t N> using Array = std::array<int, N>; template<size_t N> Array<N> random_arr(int from, int to); template<size_t N> std::eek:stream& operator<<(std::eek:stream& os, const Array<N>& arr); int main(){ Array<10> arr = random_arr<10>(7, 14); std::cout << arr << std::endl; std::for_each(arr.begin(), arr.end(), [](int& n){ if(n % 7 == 0) n = 0; }); std::cout << arr << std::endl; return 0; } template<size_t N> Array<N> random_arr(int from, int to){ Array<N> res; std::random_device rd; std::uniform_int_distribution<int> ds(from, to); for(size_t i = 0; i < N; i++){ res = ds(rd); } return res; } template<size_t N> std::eek:stream& operator<<(std::eek:stream& os, const Array<N>& arr){ os << "["; for(size_t i = 0; i < N; i++){ os << arr; if(i + 1 != N) os << ", "; } os << "]"; return os; }
 
Назад
Сверху