fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. void multiply_by_2(vector<double> &v)
  8. {
  9. int i;
  10. int n = v.size();
  11.  
  12. // multiply each coordinate by 2
  13.  
  14. for (i=0; i<n; i++)
  15. v[i] *= 2;
  16. }
  17.  
  18. int main()
  19. {
  20. int i;
  21. int n=10;
  22. vector<double> v(n);
  23.  
  24. srand48(123);
  25.  
  26. for (i=0; i<n; i++)
  27. v[i] = drand48();
  28.  
  29. cout << "old vector:" << endl;
  30. for (i=0; i<n; i++)
  31. cout << v[i] << endl;
  32.  
  33. multiply_by_2(v);
  34.  
  35. cout << "new vector:" << endl;
  36. for (i=0; i<n; i++)
  37. cout << v[i] << endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
old vector:
0.279512
0.414529
0.925176
0.129524
0.124169
0.349821
0.625824
0.833907
0.69485
0.150055
new vector:
0.559024
0.829058
1.85035
0.259048
0.248338
0.699641
1.25165
1.66781
1.3897
0.300111