1. #include <iostream>
#include <random>
#include <array>
#include <algorithm>
#include <iterator>
const unsigned short int _size = 20;
int main()
{
std::array<unsigned int, _size> arr;
std::uniform_int_distribution<unsigned int> distrib(201, 300); // от 1 до 500 числа будут в массиве
std::default_random_engine gen;
std::for_each(arr.begin(), arr.end(), [&distrib, &gen](auto& var)
var = distrib(gen);
});
std::copy(arr.begin(), arr.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
std::cout << std::endl;
std::cout << "Сумма = " << std::accumulate(arr.begin(), arr.end(), 0) << std::endl;
return 0;
}
2.
#include <iostream>
void bubble_sort(auto &array)
for(unsigned int i = 0; i < array.size(); i++)
for(unsigned int j = 0; j < array.size() - i - 1; j++)
if(array[j] < array[j + 1])
std::swap(array[j], array[j+1]);
bubble_sort(arr);//Сортировка
3.
struct Film
private:
std::string _name;
unsigned short int _year;
long int _money;
public:
void get() const
std::cout << _name << " - " << _year << " - " << _money << std::endl;
void set()
std::cin >> _name >> _year >> _money;
long int getMoney()
return _money;
};
std::array<Film, 5> arr;
for(auto& i : arr)
i.set();
std::cout << std::endl << std::endl;
i.get();
std::sort(arr.begin(), arr.end(), [](Film& var1, Film& var2)
return var1.getMoney() < var2.getMoney();
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
1. #include <iostream>
#include <random>
#include <array>
#include <algorithm>
#include <iterator>
const unsigned short int _size = 20;
int main()
{
std::array<unsigned int, _size> arr;
std::uniform_int_distribution<unsigned int> distrib(201, 300); // от 1 до 500 числа будут в массиве
std::default_random_engine gen;
std::for_each(arr.begin(), arr.end(), [&distrib, &gen](auto& var)
{
var = distrib(gen);
});
std::copy(arr.begin(), arr.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
std::cout << std::endl;
std::cout << "Сумма = " << std::accumulate(arr.begin(), arr.end(), 0) << std::endl;
return 0;
}
2.
#include <iostream>
#include <random>
#include <array>
#include <algorithm>
#include <iterator>
const unsigned short int _size = 20;
void bubble_sort(auto &array)
{
for(unsigned int i = 0; i < array.size(); i++)
for(unsigned int j = 0; j < array.size() - i - 1; j++)
{
if(array[j] < array[j + 1])
std::swap(array[j], array[j+1]);
}
}
int main()
{
std::array<unsigned int, _size> arr;
std::uniform_int_distribution<unsigned int> distrib(201, 300); // от 1 до 500 числа будут в массиве
std::default_random_engine gen;
std::for_each(arr.begin(), arr.end(), [&distrib, &gen](auto& var)
{
var = distrib(gen);
});
std::copy(arr.begin(), arr.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
std::cout << std::endl;
bubble_sort(arr);//Сортировка
std::copy(arr.begin(), arr.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
return 0;
}
3.
#include <iostream>
#include <array>
#include <algorithm>
struct Film
{
private:
std::string _name;
unsigned short int _year;
long int _money;
public:
void get() const
{
std::cout << _name << " - " << _year << " - " << _money << std::endl;
}
void set()
{
std::cin >> _name >> _year >> _money;
}
long int getMoney()
{
return _money;
}
};
int main()
{
std::array<Film, 5> arr;
for(auto& i : arr)
i.set();
std::cout << std::endl << std::endl;
for(auto& i : arr)
i.get();
std::sort(arr.begin(), arr.end(), [](Film& var1, Film& var2)
{
return var1.getMoney() < var2.getMoney();
});
std::cout << std::endl << std::endl;
for(auto& i : arr)
i.get();
return 0;
}