Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to read from a binary data file (Lab4.dat). Sort the data in asc

ID: 3702829 • Letter: W

Question

Write a program to read from a binary data file (Lab4.dat). Sort the data in ascending order (smallest to largest). Find the minimum value, maximum value, median value, mean, and standard deviation of the data set.

Lab4.dat is a binary data file that contains 200 doubles in random order, valued in the range (-1.0, 1.0). So I expect the mean pretty close to zero. The min should be element zeroth, the max element 199th, and the median element 99th.

See Fig 7.2, page 384-385 in the text for how to set up your for loop to computer the mean (average) and standard deviation.

You have to modify the swap and sort functions to take in doubles instead of integers.

Once you have the array sorted, print them out to the console, 10 elements per row. So I expect to see 20 rows of data - sorted, see attached bitmap file for how to print your sorted data. Hint: use this format string if you want your data to look like mine:

printf("% 02.4f ", bin_arr[i]);

After printing your sorted data, also print the min, max, median, mean, and standard deviation

Here is what someone has helped me with so far - the problem is that even though it is compiling successfully, it keeps crashing when i run it, can someone tell me what is going on?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*Insertion Sort with n size*/
void sort(double a[], int n)
{
int i,j; /* Declare integer i and j variables*/
double temp; /*Declare double variable temp*/
for( i=1; i<n; i++) /* create for loop until n*/
{
temp = a[i]; /* store second element of array to temp variable*/
j = i-1; /* assign first position of an array element*/
while(j>=0 && a[j] > temp) /* create while loop check condition first element > 0 and first element > second element then*/
{
/* insertion process is continued*/
a[j+1] = a[j];
j = j- 1;
}
a[j+1] = temp; /* temp value is stored int next elements*/
}
}
/* Create caclMeanSTD() function to caclulate mean and standard deviation*/
void calcMeanSTD(double x[],int n)
{
double sum=0.0,mean,std=0.0; /* declare and initialize double variables*/
int i,n1; /* Declare integer variables*/
for(i=0;i<n;i++) /* create for loop until size of an array*/
sum+=x[i]; /* calculate sum of an array*/
mean=sum/n; /* calculate mean value*/
printf(" Mean: %.2lf",mean); /* display mean value*/
for(i=0;i<n;i++) /* create for loop until size of an array*/
std+=pow(x[i]-mean,2); /* calculate standard deviation sum*/
printf(" Standard Deviation: %.2lf",sqrt(std/n)); /* calculate standard deviation value and display*/
}
/* Create findMaxMin() function to find the max and min values*/
void findMaxMin(double a[],int n)
{
double max,min; /* declare double variables*/
int i; /* declare integer variable*/
max=a[0]; /* assign max and min of first element from the array*/
min=a[0];
for(i=0;i<n;i++) /* create for loop until size of an array*/
{
if(a[i]>max) max=a[i]; /*check condition maximum element and stored max*/
else
if(a[i]<min) min=a[i]; /* check condition minimum element and stored min*/
}
/* display maximum and minimum element of an array*/
printf(" Maximum= %.2lf",max);
printf(" Minimum= %.2lf",min);
}
int main()
{
FILE *fp; /* Declare File Pointeer*/
int i,k,n1; /* declare integer variable*/
double rd,arr[200]; /* declare double variable rd and an array "arr"*/
int count=0,t; /* declare integer variables*/
fp=fopen("F:\Lab4.dat","rb"); /* open binary file in read mode*/
for(i=0;i<200;i++) /* create for loop until 200 elements*/
{
fread(&rd,sizeof(rd),1,fp); /* read one by one elements from the file*/
arr[k++]=rd; /* store read elements to array "arr"*/
count++; /* calcualte total number of elements in the file*/
}
n1=count; /* assign total elements into n1 for calculating Median*/
sort(arr,count); /* call sort() function*/
printf(" Sorted Elements are ");
for(i=0;i<count;i++) /* create for loop until total elements in the file*/
{
if(i%10==0) printf(" "); /* display 10 elements in each row*/
printf("%.2lf ",arr[i]); /* display array elements*/
}
printf(" ");
calcMeanSTD(arr,count); /* call calcMeanSTD() function*/
printf(" ");
findMaxMin(arr,count); /* call findMaxMin() function*/
printf(" ");
n1=(n1+1)/2-1; /* calculate median element*/
printf(" Median= %.2lf",arr[n1]); /* display the Median element of an array*/
fclose(fp); /* close file*/
return 0;
}

Explanation / Answer

You opened the file using fopen and directly started reading from it using fread,but you forgot to check wheather the file is actually opened . Since you have not provided the Lab4.dat file . I can't atually test the file .

But what seems to me is that for some reason you file is not opened. To check wheather your file is opened or not after fopen use a if statement .

Ex :- if(fp!=NULL){

Rest part of the code----------------------

}

Please comment if you need more help.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote