Advertisement
sparkychild

Untitled

Oct 20th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. //prototype for the user information function.
  9. //prototype for the standard deviation function.
  10. //prototype for function that finds the high value
  11. //prototype for function that finds the low value
  12. double findMean(const double list[], int numOfElements);
  13. double findStdDev(const double list[], int numOfElements);
  14. double findBiggest(const double list[], int size);
  15. double findSmallest(const double list[], int size);
  16.  
  17. double findMean(double list[], int numOfElements)
  18. {
  19. int index=0;
  20. double sum = 0;
  21.  
  22. for (index = 0; index < numOfElements; index++)
  23. sum = sum + list[index];
  24.  
  25. return sum / numOfElements;
  26. }
  27. ///==================================================
  28. double findStdDev(const double list[], int numOfElements){
  29. //lesse if i can get mean this way instead then
  30. int indexM=0;
  31. double sumM = 0;
  32.  
  33. for (indexM = 0; indexM < numOfElements; indexM++)
  34. sumM = sumM + list[indexM];
  35.  
  36. double mean = sumM;
  37. double numerator=0, stdDev=0;
  38. int index=0;
  39.  
  40. for (index=0; index <numOfElements; index++)
  41. numerator = numerator + pow(list[index] - mean,2);
  42.  
  43. stdDev = sqrt(numerator / numOfElements);
  44. return stdDev;
  45. } // define the function to calculate standared deviation
  46.  
  47. ///==================================================
  48. double findSmallest(const double list[], int size){ // define the function to determine the smallest value
  49. // in the array
  50. double smallest = list[0];
  51. for (int index =0; index<size; index++){
  52. if (list[index]<smallest){
  53. smallest = list[index];
  54. }
  55. }
  56. return smallest;
  57. }
  58.  
  59. ///==================================================
  60. double findBiggest(const double list[], int size){ // define the function to determine the largest value
  61. // in the array.
  62. double biggest = list[0];
  63. for (int index =0; index<size; index++){
  64. if (list[index]>biggest){
  65. biggest = list[index];
  66. }
  67. }
  68. return biggest;
  69. }
  70.  
  71. ///==================================================
  72.  
  73. ///==================================================
  74. int main()
  75. {
  76. double numList[100];
  77. int count=0; //total numbers read
  78.  
  79. ifstream inFile;
  80. inFile.open("lab8input.txt");
  81. if(!inFile){
  82. cout <<"\n\nINPUT FILE ERROR!\n\n";
  83. return 1;
  84. }
  85.  
  86.  
  87. cout << fixed << showpoint << setprecision(2);
  88.  
  89. inFile >> count; //read the number of items
  90. cout << "************\n"; //loop and
  91. for(int index=0; index<count; index++){ //fill the array
  92. inFile >> numList[index];
  93. }
  94.  
  95. //////////////////////////////
  96.  
  97. cout << "\nThe mean value is equal to: "<< findMean(numList, count);
  98. cout << "\nThe standard deviation is equal to: "<< findStdDev(numList, count);
  99. cout << "\nThe largest value equals: " << findBiggest(numList, count);
  100. cout << "\nThe smallest value equals: " << findSmallest(numList, count);
  101.  
  102. inFile.close();
  103. return 0;
  104. }
  105. ///==================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement