Guest User

Untitled

a guest
Dec 2nd, 2017
2,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. /*
  2. Brendon Gill
  3. brendon594@yahoo.com
  4. Lab #4
  5. */
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. //All function prototypes
  13.  
  14. float* getdata(int *numdata);
  15.  
  16. float** pointerpointing(float *fdata,int numdata);
  17.  
  18. void sort(float **f2data,int numdata);
  19.  
  20. float calc(float *fdata,int numdata,float* mean);
  21.  
  22. void output(float *fdata,float **f2data,float mean,float dev,int numdata);
  23.  
  24. /*
  25. Pre: Main calls all functions and returns data to itself.
  26. Post: At the end of main all programs cease and output is directed to file.
  27. */
  28.  
  29. int main (void)
  30.  
  31. {
  32. //Local Declarations
  33.  
  34. //Mean and Deviation
  35.  
  36. float mean=0.0,dev=0.0;
  37. //Pointer filled with data
  38. float *fdata;
  39. //Pinter that is sorted, pointing to data
  40. float **f2data;
  41. //Number of data
  42. int numdata;
  43.  
  44. fdata=getdata(&numdata);
  45.  
  46. f2data=pointerpointing(fdata,numdata);
  47.  
  48. sort(f2data,numdata);
  49.  
  50. dev=calc(fdata,numdata,&mean);
  51.  
  52. output(fdata,f2data,mean,dev,numdata);
  53.  
  54.  
  55. return 0;
  56. }
  57.  
  58. /*
  59. Pre:Opens the file and gets data
  60. Post: Returns the pointer filled with data, and the numdata filled with number
  61. of floats in the pointer.
  62. */
  63.  
  64. float* getdata(int *numdata)
  65.  
  66. {
  67. // Flodata is the data
  68.  
  69. float *flodata;
  70. int i;
  71.  
  72.  
  73.  
  74. FILE *New;
  75.  
  76.  
  77. New=fopen("jamrock.txt","r");
  78.  
  79. if(New == NULL)
  80. {
  81. printf("\n No such file");
  82. return NULL;
  83. }
  84.  
  85. fscanf (New,"%d", numdata);
  86.  
  87. //Allocating memory
  88.  
  89. flodata=(float*)calloc(*numdata,sizeof(float));
  90.  
  91. for (i=0;*numdata>i;i++)
  92. fscanf (New,"%f",(flodata+i));
  93. //Returns to main
  94.  
  95. return flodata;
  96.  
  97. }
  98.  
  99.  
  100. /*
  101. Pre: Accepts pointer to data and equates it to another pointer
  102. Post: Sets the float ** equal to the float * and returns to main
  103. */
  104. float** pointerpointing(float *fdata,int numdata)
  105. {
  106. //This will be the sorted pointer
  107. float **f2data;
  108.  
  109. int i;
  110.  
  111. f2data=(float**)calloc(numdata,sizeof(float));
  112.  
  113. for (i=0;numdata>i;i++)
  114. {
  115. *(f2data+i)=(fdata+i);
  116.  
  117. }
  118. //Returns f2data to main
  119.  
  120. return f2data;
  121. }
  122.  
  123. /*
  124. Pre: Accepts the second level pointer, and numdata and sorts
  125. Post:sends data back to main to sorted not changing orginal data
  126. */
  127. void sort(float **f2data,int numdata)
  128. {
  129. int i,j;
  130. float* temp;
  131.  
  132. for(i=0;i<numdata;i++)
  133. {
  134. for(j=i+1;j<numdata;j++)
  135. {
  136. if(**(f2data+i)>**(f2data+j))
  137. {
  138. // One star to change the address instead of the data
  139.  
  140. temp=*(f2data+i);
  141. *(f2data+i)=*(f2data+j);
  142. *(f2data+j)=temp;
  143. }
  144. }
  145. }
  146.  
  147.  
  148. return ;
  149. }
  150. /*
  151. Pre: Accepts original data and numdata, and calulates mean and
  152. standard deviation
  153. Post: sends the mean and deivation back to main
  154. */
  155. float calc(float *fdata,int numdata,float *mean)
  156. {
  157. int i;
  158. float dev=0.0,temp2=0.0;
  159.  
  160.  
  161. //To Calculate mean
  162.  
  163. for (i=0;numdata>i;i++)
  164. *mean = *mean + *(fdata+i);
  165.  
  166. *mean = *mean/numdata;
  167.  
  168. //To calculate standard deviation
  169. for (i=0;numdata>i;i++)
  170. {
  171. dev=*(fdata+i)-*mean;
  172.  
  173. dev= dev*dev;
  174.  
  175. temp2=temp2+dev;
  176. }
  177.  
  178. dev=sqrt(temp2/numdata);
  179.  
  180.  
  181.  
  182.  
  183. //Sends dev back to main
  184.  
  185. return dev;
  186. }
  187.  
  188.  
  189. /*
  190. Pre: Outputs data to new file and outpouts to screen
  191. Post: Program is finished and data is presented as needed
  192. */
  193. void output(float *fdata,float **f2data,float mean,float dev,int numdata)
  194.  
  195. {
  196. FILE* New;
  197.  
  198. int i,k;
  199.  
  200. printf ("Brendon Gill\nbrendon594@yahoo.com\nLab 4\n\n");
  201.  
  202. //One %f to print all data
  203.  
  204. for(k=0;k<numdata;k++)
  205. {
  206. if(k%8 == 0)
  207. printf("\n");
  208. printf ("%.1f\t",*(fdata+k));
  209.  
  210. }
  211. //To Space data
  212.  
  213. printf ("\n\n");
  214.  
  215. for(k=0;k<numdata;k++)
  216. {
  217. if(**(f2data+k)< (mean-(1.5*dev))||**(f2data+k)> (mean+(1.5*dev)))
  218. printf ("%.1f\tOut Of Range\n",**(f2data+k));
  219. else
  220. printf ("%.1f\n",**(f2data+k));
  221. }
  222.  
  223. //Open new document to send too
  224.  
  225. New=fopen("output.txt","w");
  226.  
  227. fprintf (New,"Brendon Gill\nbrendon594@yahoo.com\nLab 4\n\n");
  228.  
  229. for(k=0;k<numdata;k++)
  230. {
  231. if(k%8 == 0)
  232. fprintf(New,"\n");
  233. fprintf (New,"%.1f\t",*(fdata+k));
  234.  
  235. }
  236.  
  237. fprintf (New,"\n\n");
  238.  
  239. for(k=0;k<numdata;k++)
  240. {
  241. if(**(f2data+k)< (mean-(1.5*dev))||**(f2data+k)> (mean+(1.5*dev)))
  242. fprintf (New,"%.1f\tOut Of Range\n",**(f2data+k));
  243. else
  244. fprintf (New,"%.1f\n",**(f2data+k));
  245. }
  246.  
  247. //To stop in dev c++
  248.  
  249. scanf ("%d",&i);
  250.  
  251. return ;
  252. }
Add Comment
Please, Sign In to add comment