Programming in C language Write a program to read from a binary data file (Lab4.
ID: 3701467 • Letter: P
Question
Programming in C language 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]);
Explanation / Answer
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
// Iam using Insertion Sort sort() with n size
void sort(double a[], int n)
{
int i,j; // Declare integer i,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;
}
OUTPUT
Sorted Elements are
0.00 0.20 0.27 0.77 1.44 1.46 2.34 2.46 3.89 4.50
5.75 8.28 9.21 9.35 9.99 10.41 11.53 12.11 14.98 15.18
17.15 17.74 18.01 18.64 18.94 19.51 20.16 20.20 22.98 23.20
23.32 23.67 24.14 24.16 24.83 25.11 25.48 27.18 27.23 27.68
28.52 29.15 31.35 31.67 31.80 32.11 32.41 33.09 33.65 34.34
34.50 36.88 38.56 41.41 43.62 44.55 44.71 45.20 45.81 46.87
48.70 48.79 49.47 49.65 49.81 51.45 51.91 55.10 56.62 56.69
57.39 57.69 59.20 59.71 60.26 61.44 61.58 61.91 63.12 64.30
65.15 66.45 69.66 69.83 69.89 71.55 73.02 73.86 74.47 74.72
75.03 75.70 76.75 77.28 77.86 77.87 78.62 79.45 80.59 82.06
82.56 82.70 84.14 84.70 84.72 85.17 87.05 87.11 88.36 89.03
90.03 90.64 92.33 92.94 93.18 93.58 93.78 93.81 95.36 95.84
96.32 98.14 98.34 98.59 99.47 99.56 99.77 100.19 102.69 107.69
107.74 108.62 108.63 110.95 111.78 111.93 112.42 113.24 113.52 114.07
114.65 116.00 116.41 118.28 119.03 119.06 119.93 120.11 120.42 121.11
121.75 121.85 121.97 122.32 122.42 123.13 123.24 123.83 124.73 127.74
128.34 131.50 131.54 132.09 132.50 133.88 134.62 134.81 136.74 136.75
137.23 137.53 137.65 137.97 138.12 138.22 138.76 139.69 140.72 141.26
143.51 143.72 145.84 146.79 148.29 150.53 150.96 151.66 154.18 155.51
155.54 155.57 156.61 158.37 161.04 161.96 162.19 162.96 163.31 163.79
Mean: 80.30
Standard Deviation: 47.50
Maximum= 163.79
Minimum= 0.00
Median= 82.06
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.