Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a C program called stats that reads doubles from a file given as a command line argument, puts them into an array, and computes
Write a C program called stats that reads doubles from a file given as a command line argument, puts them into an array, and computes statistics for them. You will need to read the file twice. The first time, just count the number of input items. Then you can create the array and read the items into the array. After reading the file to count the numbers, use rewind to reset the file position to the beginning so that you can read the numbers again. You will define the following functions: - int count(FILE *in) will count the items in the input stream. - void read_numbers(FILE*in, int n, double a[n] ) will read the numbers into the array. - double sum(int n, double a[n] ) will return the sum of the numbers in the array. - double mean(int n, double a[n] ) will return the mean (average) of the numbers in the array. - double variance(int n, double a[n] ) will return the variance of the numbers in the array. - double stddev(int n, double a[n] ) will return the standard deviation of the numbers in the array. - double median(int n, double a[n] ) will return the median. The standard deviation is the square root of the variance. You can compute the square root using the function sqrt. The variance is the following sum: ((a[0])2+(a[1])2++(a[n1])2) where is the mean. The median is the middle number in the array if the size is odd and the average of the two middle numbers in the array if the size is even. To compute the median, sort the array using qsort, which is an implementation of quicksort. qsort requires a comparison function which is tricky. Here is one that will work: You will call qsort like this: qsort(a, n, sizeof(double), compare); Your output should look like this: Print the labels in a field of width 10 and the numbers in a field of width 20. To compile the program, the cc command will need the option -Im to include the math library (for sqrt). If you make sure the files .bashrc and .profile in your home directory contain the following lines, this will happen automatically. exportLDLIBS=1mexportCFLAGS="-g" The second line enables you to use the debugger (more on this later)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started