7. First use an editor to create a file with some integer numbers. Now write a program, which reads these numbers and determines their mean and standard deviation.
FLOW CHART :-
Algorithm:
Step – 1 : Start
Step – 2 : Initialize sum = 0, sum_sq = 0, count = 0, mean = 0 and sd = 0
Step – 3 : Open a file ‘mean.dat’ in write mode
Step – 4 : Check the condition if(fp==NULL)
Step – 5 : Print the integers
Step – 6 : To Stop entry press Ctrl + z
Step – 7 : If end of the input is reached return EOF
Step – 8 : Close the file
Step – 9 : Open the file ‘mean.dat’ in read mode
Step – 10 : Print file to be open if(fp==NULL) condition is true and exit if file ended
Step – 11 : Print numbers in file
Step – 12 : If check the condition while end is not reached
Step – 13 : If true
(i) Read integer numbers from file
(ii) Print the number
(iii) Increment sum by no.
(iv) Increment count by 1.
(v) Increment sum by no. * no.
Step – 14 : Close the file
Step – 15 : Mean = sum / count
Step – 16 : Sd = sqrt((sum_sq / count )–(mean * mean))
Step – 17 : Print mean, standard deviation
Step – 18 : Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{ FILE *fp;
int n, count = 0, sum = 0, sumsq = 0;
float mean = 0.0, sd = 0.0;
clrscr();
fp = fopen("mean.dat","w");
if(fp==NULL)
{
puts("\n Can't open the file ");
exit(0); }
printf("\n Enter some numbers. To stop use Ctrl + z :\n");
while(scanf("%d",&n)!=EOF)
{ fprintf(fp,"%d\t",n); }
fclose(fp);
fp = fopen("mean.dat","r");
printf("\n The numbers stored in file : \n");
while(fscanf(fp,"%d",&n)!=EOF)
{ count++;
sum = sum + n;
printf("%d\t",n);
sumsq += n * n;
}
fclose(fp);
mean = sum / count;
printf("\n Mean = %0.2f",mean);
sd = sqrt((sumsq/count)-(mean*mean));
printf("\n standard derivation = %0.2f\n",sd);
getch(); return 0;
}
THIS CODE IS CREATED BY ECE THE WONDERS, HEMU THE SMART
Output:
Enter some numbers to stop use Ctrl + z:
1.2
2.1
2.3
7.8
5.6
3.4
3.1
9.0
^Z
The numbers stored in file :
1.200000 2.100000 2.300000 7.800000 5.600000
3.400000 3.100000 9.000000
Mean = 4.31
Standard derivation = 2.66
0 Comments