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

Hello, if you could do this in C, that would be great. Write a program that comp

ID: 3631234 • Letter: H

Question

Hello, if you could do this in C, that would be great.

Write a program that computes the average and standard deviation of a set of double
values read from a file. The standard deviation of the set of values is
given by the expression:

S = sqrt[ ((xi)2)/N - (x-bar)2], where i = 1


where is the average value, defined by the expression:

x-bar = (xi)/N, where i = 1

Your program should read data from a file which contains one number per line. The total number of lines is not known before execution of the program. The program should detect the end of file to stop reading the data. It should conform to the following format:

$ ./sdev < file.dat
The average is 23.8733
The standard deviation is 1.4754

Hint: accumulate the sum of values and the sum of squares in the loop reading data, then compute and S at the end.
Submit your program called sdev.c using the handin command

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
int main()
{
char *inname = "file.dat";
FILE *infile;
double num[10];
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
char line_number;
double sum=0,sqsum=0;
infile = fopen(inname, "r");
if (!infile) {
printf("Couldn't open file %s for reading. ", inname);
return 0;
}
printf("Opened file %s for reading. ", inname);

line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
++line_number;
/* note that the newline is in the buffer */
num[line_number]=atof(line_buffer);
sum= sum+ num[line_number] ;
sqsum=sqsum+num[line_number]*num[line_number];
}
float stddiv;
stddiv=sqrt(sqsum/line_number-(sum/line_number)*(sum/line_number));
printf("avrerage =%f ",sum/line_number) ;
printf(" standard devaition = %f ",stddiv);
return 0;
}

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote