-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealarray.cpp
More file actions
58 lines (47 loc) · 1.09 KB
/
realarray.cpp
File metadata and controls
58 lines (47 loc) · 1.09 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <random>
template <typename Iterator>
void print_range( Iterator begin, Iterator end )
{
std::cout << "{ ";
for( auto it = begin; it != end; ++it )
{
std::cout << *it;
if( it + 1 != end )
{
std::cout << ",";
}
std::cout << " ";
}
std::cout << "}" << std::endl;
}
float add( std::vector<float> *vec, int i, float y )
{
(*vec)[i] += y;
return (*vec)[i];
}
float partial_sum( std::vector<float> *vec, int i )
{
float result = 0.0;
for( int j = 0; j < i; ++j )
{
result += (*vec)[i];
}
return result;
}
int main( int argv, char *argc[] )
{
std::random_device rd;
std::mt19937 gen( rd() );
std::vector<float> data;
for( int i = 0; i < 20; ++i )
{
data.push_back( (float) i / 2.0 );
}
std::shuffle( data.begin(), data.end(), gen );
print_range( data.begin(), data.end() );
std::cout << add( &data, 3, 2.3 ) << std::endl;
print_range( data.begin(), data.end() );
std::cout << partial_sum( &data, 5 );
}