fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void create(double *&x,int n){
  7. x=new double[n]();
  8. }
  9. void enter(double *x,int n){
  10. for(int i=0;i<n;++i){
  11. cin>>x[i];
  12. }
  13. }
  14. void print(double *x,int n){
  15. for(int i=0;i<n;++i){
  16. cout<<x[i]<<" ";
  17. }
  18. }
  19. void add(double *x,double *y,double *z,int n){
  20. for(int i=0;i<n;++i){
  21. z[i]=x[i]+y[i];
  22. }
  23. }
  24. void subtract(double *x,double *y,double *z,int n){
  25. for(int i=0;i<n;++i){
  26. z[i]=x[i]-y[i];
  27. }
  28. }
  29. double length(double *x,int n){
  30. double sum=0;
  31. for(int i=0;i<n;++i){
  32. sum+=x[i]*x[i];
  33. }
  34. return(sqrt(sum));
  35. }
  36. void normalize(double *x,double *z,int n){
  37. double sum=0;
  38. for(int i=0;i<n;++i){
  39. sum+=x[i]*x[i];
  40. }
  41. double length=sqrt(sum);
  42. if(length==0)return;
  43. for(int i=0;i<n;++i){
  44. z[i]=x[i]/length;
  45. }
  46. }
  47. double dot(double *x,double *y,int n){
  48. double sum=0;
  49. for(int i=0;i<n;++i){
  50. sum+=(x[i]*y[i]);
  51. }
  52. return(sum);
  53. }
  54. double mean(double *x,int n){
  55. if(n==0)return 0;
  56. double sum=0;
  57. for(int i=0;i<n;++i){
  58. sum+=x[i];
  59. }
  60. return(sum/n);
  61. }
  62. double distance(double *x,double *y,int n){
  63. double sum=0;
  64. for(int i=0;i<n;++i){
  65. sum+=(x[i]-y[i])*(x[i]-y[i]);
  66. }
  67. return(sqrt(sum));
  68. }
  69.  
  70. int main() {
  71. double *x,*y,*z;
  72. int n;
  73. cin>>n;
  74. create(x,n);
  75. enter(x,n);
  76. create(y,n);
  77. enter(y,n);
  78. create(z,n);
  79. cout<<"a = ( ";
  80. print(x,n);
  81. cout<<")"<<endl;
  82. cout<<"b = ( ";
  83. print(y,n);
  84. cout<<")"<<endl;
  85. add(x,y,z,n);
  86. cout<<"a + b = ( ";
  87. print(z,n);
  88. cout<<")"<<endl;
  89. subtract(x,y,z,n);
  90. cout<<"a - b = ( ";
  91. print(z,n);
  92. cout<<")"<<endl;
  93. cout<<"|a| = "<<length(x,n)<<endl;
  94. cout<<"a * b = "<<dot(x,y,n)<<endl;
  95. cout<<"mean of a = "<<mean(x,n)<<endl;
  96. cout<<"distance (a,b) = "<<distance(x,y,n)<<endl;
  97. normalize(x,z,n);
  98. cout<<fixed<<setprecision(2);
  99. cout<<"normalized a = ( ";
  100. print(z,n);
  101. cout<<")"<<endl;
  102. return 0;
  103. }
Success #stdin #stdout 0.01s 5300KB
stdin
10
0 2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27 30
stdout
a = ( 0 2 4 6 8 10 12 14 16 18 )
b = ( 3 6 9 12 15 18 21 24 27 30 )
a + b = ( 3 8 13 18 23 28 33 38 43 48 )
a - b = ( -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 )
|a| = 33.7639
a * b = 1980
mean of a = 9
distance (a,b) = 25.3969
normalized a = ( 0.00 0.06 0.12 0.18 0.24 0.30 0.36 0.41 0.47 0.53 )