daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. double getAmount ();
  6. double roundAmount (double exactAmount);
  7. double calcAverage (double sum , int count);
  8. double calcStdDev (double sum , double sumSquared , int count);
  9. double calcDif (double exactSum , double roundedSum);
  10.  
  11. using namespace std;
  12.  
  13.  
  14. //******************************************************
  15.  
  16.  
  17.  
  18. int main ()
  19. {
  20. double count = 0;
  21. double exactAmount = 0;
  22. double exactSum = 0;
  23. double roundedAmount = 0;
  24. double roundedSum = 0;
  25. double exactSumSquared = 0;
  26. double roundedSumSquared = 0;
  27. double exactStdDev;
  28. double roundedStdDev;
  29. double exactAverage;
  30. double roundedAverage;
  31. double percentDif;
  32.  
  33. exactAmount = getAmount ();
  34. roundedAmount = roundAmount (exactAmount);
  35. exactSumSquared = exactSum * exactSum;
  36. roundedSumSquared = roundedSum * roundedSum;
  37.  
  38. while (exactAmount > 0)
  39. {
  40. count++;
  41. exactSum = exactSum + exactAmount;
  42. roundedSum = roundedSum + roundedAmount;
  43.  
  44. exactAverage = calcAverage (exactSum , count);
  45. roundedAverage = calcAverage (roundedSum , count);
  46.  
  47. exactStdDev = calcStdDev (exactSum , exactSumSquared , count);
  48. roundedStdDev = calcStdDev (roundedSum , roundedSumSquared , count);
  49.  
  50. percentDif = calcDif (exactSum , roundedSum);
  51.  
  52. exactAmount = getAmount ();
  53. }
  54.  
  55. cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2) << exactSum
  56.      << exactAverage << exactStdDev;
  57.  
  58.  
  59. cout << endl;
  60. cout << "Calculation       Exact       Rounded" << endl;
  61. cout << "---------------------------------------" << endl;
  62. cout << "Sum:          " << exactSum << "           " << roundedSum << endl;
  63. cout << "---------------------------------------" << endl;
  64. cout << "Average:            " << exactAverage << "          " << roundedAverage << endl;
  65. cout << "---------------------------------------" << endl;
  66. cout << "Std Dev:            " << exactStdDev << "            " << roundedStdDev << endl;
  67.  
  68. return 0;
  69. }
  70.  
  71.  
  72.  
  73. //****************************************************
  74.  
  75.  
  76. double getAmount ()
  77. {
  78. double userVal;
  79. cout << "Enter a number (0 to quit): ";
  80. cin >> userVal;
  81.  
  82. return userVal;
  83. }
  84.  
  85. //*****
  86.  
  87. double roundAmount (double exactSum)
  88. {
  89. double roundedAmount;
  90. roundedAmount = int((exactSum + 5) / 10) * 10;
  91. return roundedAmount;
  92. }
  93.  
  94. //*****
  95.  
  96. double calcAverage (double sum , int count)
  97. {
  98. double Average;
  99. if (count > 0)
  100. {
  101. Average = sum / count;
  102. return Average;
  103. }
  104. else
  105. {
  106. return -1;
  107. }
  108. }
  109. //*****
  110.  
  111. double calcStdDev (double sum , double sumSquared , int count)
  112. {
  113. double StdDev;
  114. if (count > 1)
  115. {
  116. StdDev = sqrt ((sumSquared) * (sum * sum)) / count;
  117. return StdDev;
  118. }
  119. else
  120. {
  121. return -1;
  122. }
  123. }
  124. //*****
  125.  
  126. double calcDif (double exactSum , double roundedSum)
  127. {
  128. double Difference;
  129. Difference = ((fabs(exactSum - roundedSum)) / (exactSum)) * 100;
  130. return Difference;
  131. }
  132.  
  133. //*****
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top