Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Zach Lee
- suscansanoba@yahoo.com
- lab#4:Scientific Data Analysis
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void bubbleSort(double* array,double**arr, int howmany);
- double* getData(FILE* fpIn, double* array, double** arr,int* howmany);
- void output(int howmany, double* array, double** arr);
- double** allocate(int howmany, double* array, double** arr);
- int main(void)
- {
- double* array;
- double** arr;
- int howmany;
- FILE* fpIn;
- if((fpIn=fopen("data.txt","r"))== NULL)
- {
- printf("\n No such file");
- exit(100);
- }
- array = getData(fpIn,array,arr,&howmany);
- arr = allocate(howmany,array,arr);
- output(howmany, array, arr);
- int temp;
- printf("\nEnter an integer and press Enter to exit the program: ");
- scanf("%d", &temp);
- return 0 ;
- }
- /* This function reads data from a text file named "data.txt". First reads
- in the number of data reading, allocates memory and inputs actual data to the
- array that was passed in as an argument
- pre: pointer to file, pointer to array, pointer to pointer to array,
- and the number of data readings
- post: assigns value to howmany, and returns the pointer to the array after
- filling it with actual data
- */
- double* getData(FILE* fpIn, double* array, double** arr,int* howmany){
- fscanf(fpIn, "%d", &*howmany);
- array = (double*)calloc(*howmany, sizeof(double));
- int i = 0;
- while(i < *howmany && fscanf(fpIn, "%lf", &*(array+i)) != EOF){
- i++;
- }
- return array;
- }
- /* This function allocates memory for an array of pointers to the actual
- data and sets these pointers pointing to the actual data.
- pre: pointers to arrays, number of readings
- post: returns the array of pointers after
- memory allocated for an array of pointers
- */
- double** allocate(int howmany, double*array, double**arr){
- arr = (double**)calloc(howmany,sizeof(double*));
- for(int s = 0; s < howmany; s++)
- *(arr + s) = array + s;
- return arr;
- }
- /* Outputs the data in original order 8 values per line using the pointer to
- pointer. Then outputs to columns the data from low to high, and the words
- "out of range" if the value is < mean - 1.5 sN or if the value > mean + 1.5 sN.
- pre: number of data readings, pointer to array, and pointer to pointer to array
- post: outputs the data, the order of data in array is changed using bubblesort
- */
- void output(int howmany, double* array, double** arr){
- printf("Zach Lee, suscansanoba@yahoo.com, lab#4 Scientific Data Analysis\n");
- int z;
- int counter = 0;
- int tf = 0;
- for( z = 0; z < howmany; z++){
- printf("%.1lf ", *(*arr + z));
- counter++;
- if(counter == 8){
- printf("\n");
- counter = 0;
- }
- }
- printf("\n");
- bubbleSort(array,arr,howmany);
- double mean = 0;
- for(z = 0; z < howmany; z++)
- mean += *(*arr+z);
- mean /= howmany;
- double SN = 0;
- for(z = 0; z < howmany; z++)
- SN += (*(*arr+z) - mean) * (*(*arr+z) - mean);
- SN /= howmany;
- SN = sqrt(SN);
- for(z = 0; z < howmany; z++){
- if(*(*arr+z) < mean - 1.5 * SN || *(*arr+z) > mean + 1.5 * SN)
- printf("out of range\n");
- else
- printf("%lf\n", *(*arr+z));
- }
- }
- /*This function takes in pointer to array, pointer to pointer to array,
- and the number of data readings and sorts the data in array from lowest to highest
- pre: the pointers to the arrays and number of readings
- post: the data in the array all in order from lowest to
- */
- void bubbleSort(double* array, double** arr, int howmany){
- int tf = 0;
- int i;
- int d;
- double temp;
- while(tf == 0){
- tf = 1;
- for(i = 0; i < howmany; i++){
- if(*(*arr+i) > *(*arr+(i+1)) ){
- temp = *(*arr+i);
- *(*arr+i) = *(*arr+(i+1));
- *(*arr+(i+1)) = temp;
- tf = 0;
- }
- }
- }
- }
- /*
- Zach Lee, suscansanoba@yahoo.com, lab#4 Scientific Data Analysis
- 164.3 201.6 226.7 63.4 71.2 210.2 94.8 147.7
- 47.9 155.5 212.5 184.9 182.4 187.8 205.4 92.1
- 168.0 241.1 161.5 198.2 60.3 59.1 64.7 141.7
- 215.1 83.2 72.5 118.0 192.3 184.9 174.5 232.6
- 99.5 238.2 84.1 188.3 54.7 47.4 221.7 65.4
- 145.1 167.3 128.2 168.1 188.2 203.7 210.0 190.0
- 210.7 83.1 190.1 121.2 67.3 62.2
- out of range
- out of range
- out of range
- 54.700000
- 59.100000
- 60.300000
- 62.200000
- 63.400000
- 64.700000
- 65.400000
- 67.300000
- 71.200000
- 72.500000
- 83.100000
- 83.200000
- 84.100000
- 92.100000
- 94.800000
- 99.500000
- 118.000000
- 121.200000
- 128.200000
- 141.700000
- 145.100000
- 147.700000
- 155.500000
- 161.500000
- 164.300000
- 167.300000
- 168.000000
- 168.100000
- 174.500000
- 182.400000
- 184.900000
- 184.900000
- 187.800000
- 188.200000
- 188.300000
- 190.000000
- 190.100000
- 192.300000
- 198.200000
- 201.600000
- 203.700000
- 205.400000
- 210.000000
- 210.200000
- 210.700000
- 212.500000
- 215.100000
- 221.700000
- 226.700000
- 232.600000
- out of range
- Enter an integer and press Enter to exit the program:
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement