@Nameless One
5781 / 3430 / 255
Регистрация: 08.02.2010
Сообщений: 7,448
|
27.05.2011, 12:32
|
|
1.
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| #include <string>
#include <algorithm>
#include <iostream>
#include <array>
#include <iterator>
int main()
{
const int l_t = -5;
const int r_t = 5;
srand(static_cast<size_t>(time(NULL)));
std::array<int, 6> arr1;
auto random = [](){ return rand() % (r_t - l_t + 1) + l_t;};
std::generate(arr1.begin(), arr1.end(), random);
std::cout << "Initial array:" << std::endl;
std::copy(arr1.begin(), arr1.end(), std::ostream_iterator<int>(std::cout, "\n"));
auto M = std::max_element(arr1.begin(), arr1.end());
std::cout << "Max element " << *M << " (no. " << (M - arr1.begin() + 1)
<< ") has been replaced with the first element: "
<< *arr1.begin() << ": " << std::endl;
std::swap(*arr1.begin(), *M);
std::copy(arr1.begin(), arr1.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
} |
|
0
|