fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. vector<double> add(vector<double> u,
  8. vector<double> v)
  9. {
  10. int i;
  11. int n = v.size();
  12. vector<double> result(n);
  13.  
  14. // add u and v and return the result
  15.  
  16. for (i=0; i<n; i++)
  17. result[i] = u[i] + v[i];
  18.  
  19. // return the result
  20.  
  21. return result;
  22. }
  23.  
  24. int main()
  25. {
  26. int i;
  27. int n=5;
  28. vector<double> u(n);
  29. vector<double> v(n);
  30. vector<double> w;
  31.  
  32. srand48(123);
  33.  
  34. for (i=0; i<n; i++)
  35. {
  36. u[i] = drand48();
  37. v[i] = drand48();
  38. }
  39.  
  40. cout << "input vector 1:" << endl;
  41. for (i=0; i<n; i++)
  42. cout << u[i] << endl;
  43.  
  44. cout << "input vector 2:" << endl;
  45. for (i=0; i<n; i++)
  46. cout << v[i] << endl;
  47.  
  48. w = add(u, v);
  49.  
  50. cout << "addition of the two vectors:" << endl;
  51. for (i=0; i<n; i++)
  52. cout << w[i] << endl;
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 2816KB
stdin
Standard input is empty
stdout
input vector 1:
0.279512
0.925176
0.124169
0.625824
0.69485
input vector 2:
0.414529
0.129524
0.349821
0.833907
0.150055
addition of the two vectors:
0.694041
1.0547
0.473989
1.45973
0.844905