fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. using namespace std;
  7. double getTimeElapsed(struct timeval end, struct timeval start)
  8. {
  9. return (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.00;
  10. }
  11. double max(double a,double b)
  12. {
  13. if (a>b) return a;
  14. else return b;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18. struct timeval t0, t1;
  19. double htime;
  20. double **laplace,prev,qa=0,accu=10e-4;
  21. int i,j,rowcol,step=0;
  22. if (argc != 3)
  23. {
  24. cin >> rowcol >> accu;
  25. }
  26. else
  27. {
  28. rowcol=atoi(argv[1]);
  29. accu=atof(argv[2]);
  30. }
  31.  
  32. {
  33. laplace = new double *[rowcol+1];
  34. for (i=0;i<=rowcol;i++)
  35. laplace[i]=new double [rowcol+1];
  36.  
  37. for (i=0;i<=rowcol;i++)
  38. for (j=0;j<=rowcol;j++)
  39. {
  40. if (j==0) laplace[i][j] = 1.0;
  41. else laplace[i][j] = 0.0;
  42. }
  43. gettimeofday(&t0, NULL);
  44. while(1)
  45. {
  46. for(i=1;i<=rowcol-1;i++)
  47. for(j=1;j<=rowcol-1;j++)
  48. {
  49. prev=laplace[i][j];
  50. laplace[i][j]=0.25*(laplace[i+1][j]+laplace[i-1][j]+laplace[i][j+1]+laplace[i][j-1]);
  51. qa=max(qa,abs(laplace[i][j]-prev));
  52. }
  53. if (qa < accu)
  54. {
  55. gettimeofday(&t1, NULL);
  56. htime=getTimeElapsed(t1,t0);
  57. cout << "Done!" << endl << "Time used: " << htime << endl;
  58. for (i=0;i<=rowcol;i++) delete [] laplace[i];
  59. delete [] laplace;
  60. exit(0);
  61. }
  62. else
  63. {
  64. step++;
  65. if (step%80==0) cout << "Iteration = " << step << " , Error= " << qa << endl;
  66. qa=0;
  67. }
  68. }
  69. }
  70. return 0;
  71. }
Success #stdin #stdout 0.48s 3480KB
stdin
100 0.000001
stdout
Iteration = 80 ,  Error= 0.00289647
Iteration = 160 ,  Error= 0.0014614
Iteration = 240 ,  Error= 0.000976736
Iteration = 320 ,  Error= 0.00072916
Iteration = 400 ,  Error= 0.000575645
Iteration = 480 ,  Error= 0.000470196
Iteration = 560 ,  Error= 0.000392925
Iteration = 640 ,  Error= 0.000334053
Iteration = 720 ,  Error= 0.000287619
Iteration = 800 ,  Error= 0.00025024
Iteration = 880 ,  Error= 0.000219533
Iteration = 960 ,  Error= 0.000194064
Iteration = 1040 ,  Error= 0.000172578
Iteration = 1120 ,  Error= 0.000154279
Iteration = 1200 ,  Error= 0.00013856
Iteration = 1280 ,  Error= 0.000124971
Iteration = 1360 ,  Error= 0.000113118
Iteration = 1440 ,  Error= 0.000102708
Iteration = 1520 ,  Error= 9.35098e-05
Iteration = 1600 ,  Error= 8.53369e-05
Iteration = 1680 ,  Error= 7.80364e-05
Iteration = 1760 ,  Error= 7.14969e-05
Iteration = 1840 ,  Error= 6.56018e-05
Iteration = 1920 ,  Error= 6.02652e-05
Iteration = 2000 ,  Error= 5.54153e-05
Iteration = 2080 ,  Error= 5.10134e-05
Iteration = 2160 ,  Error= 4.69871e-05
Iteration = 2240 ,  Error= 4.33084e-05
Iteration = 2320 ,  Error= 3.99337e-05
Iteration = 2400 ,  Error= 3.68409e-05
Iteration = 2480 ,  Error= 3.39934e-05
Iteration = 2560 ,  Error= 3.13791e-05
Iteration = 2640 ,  Error= 2.89709e-05
Iteration = 2720 ,  Error= 2.67501e-05
Iteration = 2800 ,  Error= 2.47032e-05
Iteration = 2880 ,  Error= 2.28179e-05
Iteration = 2960 ,  Error= 2.10775e-05
Iteration = 3040 ,  Error= 1.94706e-05
Iteration = 3120 ,  Error= 1.79869e-05
Iteration = 3200 ,  Error= 1.66167e-05
Iteration = 3280 ,  Error= 1.53521e-05
Iteration = 3360 ,  Error= 1.41849e-05
Iteration = 3440 ,  Error= 1.31066e-05
Iteration = 3520 ,  Error= 1.21104e-05
Iteration = 3600 ,  Error= 1.119e-05
Iteration = 3680 ,  Error= 1.03396e-05
Iteration = 3760 ,  Error= 9.55398e-06
Iteration = 3840 ,  Error= 8.82807e-06
Iteration = 3920 ,  Error= 8.15737e-06
Iteration = 4000 ,  Error= 7.53765e-06
Iteration = 4080 ,  Error= 6.96505e-06
Iteration = 4160 ,  Error= 6.43597e-06
Iteration = 4240 ,  Error= 5.94711e-06
Iteration = 4320 ,  Error= 5.49539e-06
Iteration = 4400 ,  Error= 5.078e-06
Iteration = 4480 ,  Error= 4.69232e-06
Iteration = 4560 ,  Error= 4.33594e-06
Iteration = 4640 ,  Error= 4.00664e-06
Iteration = 4720 ,  Error= 3.70236e-06
Iteration = 4800 ,  Error= 3.42119e-06
Iteration = 4880 ,  Error= 3.16137e-06
Iteration = 4960 ,  Error= 2.9213e-06
Iteration = 5040 ,  Error= 2.69945e-06
Iteration = 5120 ,  Error= 2.49446e-06
Iteration = 5200 ,  Error= 2.30503e-06
Iteration = 5280 ,  Error= 2.12999e-06
Iteration = 5360 ,  Error= 1.96825e-06
Iteration = 5440 ,  Error= 1.81878e-06
Iteration = 5520 ,  Error= 1.68067e-06
Iteration = 5600 ,  Error= 1.55305e-06
Iteration = 5680 ,  Error= 1.43512e-06
Iteration = 5760 ,  Error= 1.32614e-06
Iteration = 5840 ,  Error= 1.22544e-06
Iteration = 5920 ,  Error= 1.13239e-06
Iteration = 6000 ,  Error= 1.0464e-06
Done!
Time used: 0.490736