Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Brendon Gill
- brendon594@yahoo.com
- Lab #4
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- //All function prototypes
- float* getdata(int *numdata);
- float** pointerpointing(float *fdata,int numdata);
- void sort(float **f2data,int numdata);
- float calc(float *fdata,int numdata,float* mean);
- void output(float *fdata,float **f2data,float mean,float dev,int numdata);
- /*
- Pre: Main calls all functions and returns data to itself.
- Post: At the end of main all programs cease and output is directed to file.
- */
- int main (void)
- {
- //Local Declarations
- //Mean and Deviation
- float mean=0.0,dev=0.0;
- //Pointer filled with data
- float *fdata;
- //Pinter that is sorted, pointing to data
- float **f2data;
- //Number of data
- int numdata;
- fdata=getdata(&numdata);
- f2data=pointerpointing(fdata,numdata);
- sort(f2data,numdata);
- dev=calc(fdata,numdata,&mean);
- output(fdata,f2data,mean,dev,numdata);
- return 0;
- }
- /*
- Pre:Opens the file and gets data
- Post: Returns the pointer filled with data, and the numdata filled with number
- of floats in the pointer.
- */
- float* getdata(int *numdata)
- {
- // Flodata is the data
- float *flodata;
- int i;
- FILE *New;
- New=fopen("jamrock.txt","r");
- if(New == NULL)
- {
- printf("\n No such file");
- return NULL;
- }
- fscanf (New,"%d", numdata);
- //Allocating memory
- flodata=(float*)calloc(*numdata,sizeof(float));
- for (i=0;*numdata>i;i++)
- fscanf (New,"%f",(flodata+i));
- //Returns to main
- return flodata;
- }
- /*
- Pre: Accepts pointer to data and equates it to another pointer
- Post: Sets the float ** equal to the float * and returns to main
- */
- float** pointerpointing(float *fdata,int numdata)
- {
- //This will be the sorted pointer
- float **f2data;
- int i;
- f2data=(float**)calloc(numdata,sizeof(float));
- for (i=0;numdata>i;i++)
- {
- *(f2data+i)=(fdata+i);
- }
- //Returns f2data to main
- return f2data;
- }
- /*
- Pre: Accepts the second level pointer, and numdata and sorts
- Post:sends data back to main to sorted not changing orginal data
- */
- void sort(float **f2data,int numdata)
- {
- int i,j;
- float* temp;
- for(i=0;i<numdata;i++)
- {
- for(j=i+1;j<numdata;j++)
- {
- if(**(f2data+i)>**(f2data+j))
- {
- // One star to change the address instead of the data
- temp=*(f2data+i);
- *(f2data+i)=*(f2data+j);
- *(f2data+j)=temp;
- }
- }
- }
- return ;
- }
- /*
- Pre: Accepts original data and numdata, and calulates mean and
- standard deviation
- Post: sends the mean and deivation back to main
- */
- float calc(float *fdata,int numdata,float *mean)
- {
- int i;
- float dev=0.0,temp2=0.0;
- //To Calculate mean
- for (i=0;numdata>i;i++)
- *mean = *mean + *(fdata+i);
- *mean = *mean/numdata;
- //To calculate standard deviation
- for (i=0;numdata>i;i++)
- {
- dev=*(fdata+i)-*mean;
- dev= dev*dev;
- temp2=temp2+dev;
- }
- dev=sqrt(temp2/numdata);
- //Sends dev back to main
- return dev;
- }
- /*
- Pre: Outputs data to new file and outpouts to screen
- Post: Program is finished and data is presented as needed
- */
- void output(float *fdata,float **f2data,float mean,float dev,int numdata)
- {
- FILE* New;
- int i,k;
- printf ("Brendon Gill\nbrendon594@yahoo.com\nLab 4\n\n");
- //One %f to print all data
- for(k=0;k<numdata;k++)
- {
- if(k%8 == 0)
- printf("\n");
- printf ("%.1f\t",*(fdata+k));
- }
- //To Space data
- printf ("\n\n");
- for(k=0;k<numdata;k++)
- {
- if(**(f2data+k)< (mean-(1.5*dev))||**(f2data+k)> (mean+(1.5*dev)))
- printf ("%.1f\tOut Of Range\n",**(f2data+k));
- else
- printf ("%.1f\n",**(f2data+k));
- }
- //Open new document to send too
- New=fopen("output.txt","w");
- fprintf (New,"Brendon Gill\nbrendon594@yahoo.com\nLab 4\n\n");
- for(k=0;k<numdata;k++)
- {
- if(k%8 == 0)
- fprintf(New,"\n");
- fprintf (New,"%.1f\t",*(fdata+k));
- }
- fprintf (New,"\n\n");
- for(k=0;k<numdata;k++)
- {
- if(**(f2data+k)< (mean-(1.5*dev))||**(f2data+k)> (mean+(1.5*dev)))
- fprintf (New,"%.1f\tOut Of Range\n",**(f2data+k));
- else
- fprintf (New,"%.1f\n",**(f2data+k));
- }
- //To stop in dev c++
- scanf ("%d",&i);
- return ;
- }
Add Comment
Please, Sign In to add comment