NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
03-cxx-style-ref.cxx
Go to the documentation of this file.
1 #include <cstdio>
8 #include <vector>
9 
10 
15 void initialize(std::vector<double>& x)
16 {
17  const int n=x.size();
18  for (int i=0;i<n;i++) x[i]= 1.0/(double)(1+n-i);
19 }
20 
21 double sum_elements(std::vector<double>& x)
22 {
23  double sum=0;
24  for (int i=0;i<x.size();i++)sum+=x[i];
25  return sum;
26 }
27 
28 int main()
29 {
30  const int n=12345678;
31 
32  // Instantiate a vector of doubles of given size.
33  // The class object itself is placed on the stack,
34  // but the data is placed on the heap.
35  std::vector<double> x(n);
36 
37  initialize(x);
38  double s=sum_elements(x);
39  printf("sum=%e\n",s);
40 
41  // When leaving this scope, x is destroyed, and with
42  // this also the memory on the heap is freed.
43  return 0;
44 }
void initialize(std::vector< double > &x)
The class std::vector contains size information and can be instantiated for any data type...
double sum_elements(std::vector< double > &x)
A::value_type sum(const A &a)
Sum of array or expression.
Definition: util.ixx:81
int main()