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: 3702797 • 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.

Explanation / Answer

PROGRAM

I am Creating and using "Lab4.dat" Binary File

#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: %02.4lf",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: %02.4lf",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= %02.4lf",max);

printf(" Minimum= %02.4lf",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

// Creating Binary file using below comment code

/* fp=fopen("F:\Lab4.dat","wb");

for(i=0;i<200;i++)

{

rd=rand()%100+i;

fwrite(&rd,sizeof(rd),1,fp);

}

fclose(fp); */

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("%02.4lf ",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= %02.4lf",arr[n1]); // display the Median element of an array

fclose(fp); // close file

return 0;

}

OUTPUT


Sorted Elements are

0.0000 0.2050 0.2650 0.7650 1.4400 1.4600 2.3350 2.4550 3.8900 4.5000
5.7500 8.2750 9.2100 9.3450 9.9950 10.4100 11.5300 12.1050 14.9750 15.1750
17.1500 17.7400 18.0100 18.6400 18.9400 19.5100 20.1550 20.2050 22.9800 23.1950
23.3200 23.6700 24.1350 24.1650 24.8300 25.1050 25.4850 27.1800 27.2350 27.6850
28.5250 29.1450 31.3500 31.6700 31.7950 32.1100 32.4150 33.0850 33.6450 34.3400
34.5000 36.8800 38.5550 41.4050 43.6150 44.5450 44.7100 45.2000 45.8050 46.8700
48.7050 48.7900 49.4700 49.6500 49.8050 51.4550 51.9150 55.1000 56.6150 56.6850
57.3900 57.6900 59.2000 59.7100 60.2600 61.4350 61.5800 61.9100 63.1150 64.2950
65.1500 66.4500 69.6550 69.8300 69.8850 71.5500 73.0200 73.8550 74.4650 74.7250
75.0300 75.7050 76.7500 77.2850 77.8650 77.8700 78.6200 79.4500 80.5900 82.0650
82.5600 82.7050 84.1350 84.7050 84.7200 85.1750 87.0500 87.1050 88.3650 89.0350
90.0350 90.6350 92.3350 92.9400 93.1800 93.5800 93.7800 93.8100 95.3600 95.8450
96.3200 98.1450 98.3400 98.5900 99.4750 99.5600 99.7700 100.1850 102.6850 107.6900
107.7400 108.6200 108.6300 110.9500 111.7750 111.9300 112.4150 113.2400 113.5200 114.0650
114.6450 115.9950 116.4050 118.2750 119.0250 119.0550 119.9300 120.1050 120.4200 121.1050
121.7500 121.8500 121.9650 122.3200 122.4200 123.1300 123.2400 123.8350 124.7300 127.7350
128.3350 131.4950 131.5400 132.0900 132.5000 133.8850 134.6200 134.8100 136.7400 136.7500
137.2300 137.5300 137.6450 137.9750 138.1200 138.2200 138.7650 139.6900 140.7250 141.2650
143.5150 143.7250 145.8400 146.7900 148.2900 150.5300 150.9550 151.6650 154.1800 155.5050
155.5350 155.5750 156.6100 158.3650 161.0450 161.9550 162.1950 162.9550 163.3100 163.7850

Mean: 80.2953
Standard Deviation: 47.4973

Maximum= 163.7850
Minimum= 0.0000

Median= 82.0650

UPDATED 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: %02.4lf",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: %02.4lf",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= %02.4lf",max);

printf(" Minimum= %02.4lf",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","wb");

for(i=0;i<200;i++)

{

rd=rand()%100+i;

fwrite(&rd,sizeof(rd),1,fp);

}

fclose(fp);

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("%02.4lf ",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= %02.4lf",arr[n1]); // display the Median element of an array

fclose(fp); // close file

return 0;

}

NEW UPDATED PROGRAM

NOTE: SAVE FILE SPECIFIC DIRECTORY F: OR ANY OTHER

#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: %02.4lf",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: %02.4lf",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= %02.4lf",max);

printf(" Minimum= %02.4lf",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","wb");

for(i=0;i<200;i++)

{

rd=rand()%100+i;

fwrite(&rd,sizeof(rd),1,fp);

}

fclose(fp);

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("%02.4lf ",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= %02.4lf",arr[n1]); // display the Median element of an array

fclose(fp); // close file

return 0;

}

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