Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. /*Zach Lee
  2. suscansanoba@yahoo.com
  3. lab#4:Scientific Data Analysis
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. void bubbleSort(double* array,double**arr, int howmany);
  9. double* getData(FILE* fpIn, double* array, double** arr,int* howmany);
  10. void output(int howmany, double* array, double** arr);
  11. double** allocate(int howmany, double* array, double** arr);
  12. int main(void)
  13. {
  14. double* array;
  15. double** arr;
  16. int howmany;
  17. FILE* fpIn;
  18. if((fpIn=fopen("data.txt","r"))== NULL)
  19. {
  20. printf("\n No such file");
  21. exit(100);
  22. }
  23. array = getData(fpIn,array,arr,&howmany);
  24. arr = allocate(howmany,array,arr);
  25. output(howmany, array, arr);
  26. int temp;
  27. printf("\nEnter an integer and press Enter to exit the program: ");
  28. scanf("%d", &temp);
  29. return 0 ;
  30. }
  31. /* This function reads data from a text file named "data.txt". First reads
  32. in the number of data reading, allocates memory and inputs actual data to the
  33. array that was passed in as an argument
  34. pre: pointer to file, pointer to array, pointer to pointer to array,
  35. and the number of data readings
  36. post: assigns value to howmany, and returns the pointer to the array after
  37. filling it with actual data
  38. */
  39. double* getData(FILE* fpIn, double* array, double** arr,int* howmany){
  40. fscanf(fpIn, "%d", &*howmany);
  41. array = (double*)calloc(*howmany, sizeof(double));
  42. int i = 0;
  43. while(i < *howmany && fscanf(fpIn, "%lf", &*(array+i)) != EOF){
  44. i++;
  45. }
  46. return array;
  47. }
  48. /* This function allocates memory for an array of pointers to the actual
  49. data and sets these pointers pointing to the actual data.
  50. pre: pointers to arrays, number of readings
  51. post: returns the array of pointers after
  52. memory allocated for an array of pointers
  53. */
  54. double** allocate(int howmany, double*array, double**arr){
  55. arr = (double**)calloc(howmany,sizeof(double*));
  56. for(int s = 0; s < howmany; s++)
  57. *(arr + s) = array + s;
  58. return arr;
  59. }
  60.  
  61. /* Outputs the data in original order 8 values per line using the pointer to
  62. pointer. Then outputs to columns the data from low to high, and the words
  63. "out of range" if the value is < mean - 1.5 sN or if the value > mean + 1.5 sN.
  64. pre: number of data readings, pointer to array, and pointer to pointer to array
  65. post: outputs the data, the order of data in array is changed using bubblesort
  66. */
  67. void output(int howmany, double* array, double** arr){
  68. printf("Zach Lee, suscansanoba@yahoo.com, lab#4 Scientific Data Analysis\n");
  69. int z;
  70. int counter = 0;
  71. int tf = 0;
  72. for( z = 0; z < howmany; z++){
  73. printf("%.1lf ", *(*arr + z));
  74. counter++;
  75. if(counter == 8){
  76. printf("\n");
  77. counter = 0;
  78. }
  79. }
  80. printf("\n");
  81. bubbleSort(array,arr,howmany);
  82. double mean = 0;
  83. for(z = 0; z < howmany; z++)
  84. mean += *(*arr+z);
  85. mean /= howmany;
  86. double SN = 0;
  87. for(z = 0; z < howmany; z++)
  88. SN += (*(*arr+z) - mean) * (*(*arr+z) - mean);
  89. SN /= howmany;
  90. SN = sqrt(SN);
  91. for(z = 0; z < howmany; z++){
  92. if(*(*arr+z) < mean - 1.5 * SN || *(*arr+z) > mean + 1.5 * SN)
  93. printf("out of range\n");
  94. else
  95. printf("%lf\n", *(*arr+z));
  96. }
  97. }
  98. /*This function takes in pointer to array, pointer to pointer to array,
  99. and the number of data readings and sorts the data in array from lowest to highest
  100. pre: the pointers to the arrays and number of readings
  101. post: the data in the array all in order from lowest to
  102. */
  103. void bubbleSort(double* array, double** arr, int howmany){
  104. int tf = 0;
  105. int i;
  106. int d;
  107. double temp;
  108. while(tf == 0){
  109. tf = 1;
  110. for(i = 0; i < howmany; i++){
  111. if(*(*arr+i) > *(*arr+(i+1)) ){
  112. temp = *(*arr+i);
  113. *(*arr+i) = *(*arr+(i+1));
  114. *(*arr+(i+1)) = temp;
  115. tf = 0;
  116. }
  117. }
  118. }
  119. }
  120. /*
  121. Zach Lee, suscansanoba@yahoo.com, lab#4 Scientific Data Analysis
  122. 164.3 201.6 226.7 63.4 71.2 210.2 94.8 147.7
  123. 47.9 155.5 212.5 184.9 182.4 187.8 205.4 92.1
  124. 168.0 241.1 161.5 198.2 60.3 59.1 64.7 141.7
  125. 215.1 83.2 72.5 118.0 192.3 184.9 174.5 232.6
  126. 99.5 238.2 84.1 188.3 54.7 47.4 221.7 65.4
  127. 145.1 167.3 128.2 168.1 188.2 203.7 210.0 190.0
  128. 210.7 83.1 190.1 121.2 67.3 62.2
  129. out of range
  130. out of range
  131. out of range
  132. 54.700000
  133. 59.100000
  134. 60.300000
  135. 62.200000
  136. 63.400000
  137. 64.700000
  138. 65.400000
  139. 67.300000
  140. 71.200000
  141. 72.500000
  142. 83.100000
  143. 83.200000
  144. 84.100000
  145. 92.100000
  146. 94.800000
  147. 99.500000
  148. 118.000000
  149. 121.200000
  150. 128.200000
  151. 141.700000
  152. 145.100000
  153. 147.700000
  154. 155.500000
  155. 161.500000
  156. 164.300000
  157. 167.300000
  158. 168.000000
  159. 168.100000
  160. 174.500000
  161. 182.400000
  162. 184.900000
  163. 184.900000
  164. 187.800000
  165. 188.200000
  166. 188.300000
  167. 190.000000
  168. 190.100000
  169. 192.300000
  170. 198.200000
  171. 201.600000
  172. 203.700000
  173. 205.400000
  174. 210.000000
  175. 210.200000
  176. 210.700000
  177. 212.500000
  178. 215.100000
  179. 221.700000
  180. 226.700000
  181. 232.600000
  182. out of range
  183.  
  184. Enter an integer and press Enter to exit the program:
  185. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement