This question has already been asked by someone else and been answered. The answ
ID: 3550094 • Letter: T
Question
This question has already been asked by someone else and been answered. The answer includes BUFSIZ and other things that I donot understand. Is it possible to do this question without including stdlib.h? I have been trying this for a day. Thank you.
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<conio.h>
#include<math.h>
int main()
{
double i;
FILE *fptr1;
double square=0,bar,N=0,ans;
if ((fptr1 = fopen("input.txt","r")) == NULL)
{
printf("Unable to open - file not found ");
return;
}
while(fscanf(fptr1,"%ls",i) !=EOF)
{
square = square + i*i;
bar = bar + i;
N++;
}
bar = bar/N ;
bar = bar * bar;
square = square/N;
ans = square - bar;
ans = pow(square,0.5);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.