The following program reads a specified file that contains a list of numbers. The program then prints:
Sum, sum of the sqaures
Mean, mean of the squares
Standard Deviation
A table of every number in the file with its difference from the mean and the quantity.
A second table printing how many of the numbers are within 1 S.D from the mean, between 1 and 2 S.Ds from the mean, 2 and 3 S.Ds from the mean and more than 3 S.Ds from the mean.
My problem is that when I have more than 12 values in my .txt file the program prints garbage/lots of random numbers where they shouldn't be etc. Could someone please tell me what is wrong with my code or give me some tips as to where I am going wrong?
My code is below (note: I have put several for loops instead of one in the STANDARD DEVIATION FROM THE MEAN FUNCTION as I was having problems seeing in the output which values belonged to which for loop. I am also having problems with this as I need to count the number of values not show the values. Hope everything makes sense and sorry if my code is messy as I am still learning)
#include <stdio.h>
#include <math.h>
//calculates standard deviation from the mean
//x[] = number array, a = Mean, b = Standard Deviation, n = limit
float count(float x[], float a, float b, int n);
///////////////////////TABLE FUNCTION////////////////////////
//chat r is an indicater between the two tables to be printed
float table(float x[], float a, float b, int n, int c, int r)
{
int i;
float x1, x2;
//Top column for difference from the mean and quantity table
if(r==0)
{
printf("Number\t\t1\t\t2");
}
//Top column for standard deviation from the mean table
if(r==1)
{
printf("1\t2\t3\t4");
}
//Separator '='
printf("\n");
for(i=0; i<=(c+1)*10; ++i)
printf("=");
printf("\n");
//Rows for calucations
for(i=0; i<n; i++)
{
x1 = x[i]-a;//difference from mean calculation
x2 = x1/b;//quantity calculation
//for difference from mean and quantity table
if(r==0)
{
printf("%g\t|\t%.4g\t\t%.4g\n", x[i], x1, x2);
}
}
//standard deviation from the mean table
if(r==1)
{
count(x,a,b,n);
}
printf("\n");
}
//////////////////////////////////////////////////////////
///STANDARD DEVIATION FROM THE MEAN FUNCTION///
float count(float x[], float a, float b, int n)
{
int x1=4,x2=3,x3=1,x4=2;
int i=0;
//for(i=0;i<n;i++)
for(i=0; i<n; i++)
{
if(x[i]>a-b&&x[i]<a+b)
{
printf("%.2f\n", x[i]);
}
else
{
printf("0");
}
}
printf("\n");
for(i=0; i<n; i++)
{
if(x[i]>a-2*b&&x[i]<a-b)
{
printf("%.2f\n", x[i]);
}
else
{
printf("0");
}
}
printf("\n");
for(i=0; i<n; i++)
{
if(x[i]>a-2*b&&x[i]<a-3*b)
{
printf("%.2f\n", x[i]);
}
else
{
printf("0");
}
}
printf("\n");
for(i=0; i<n; i++)
{
if(x[i]>3*b)
{
printf("%.2f\n", x[i]);
}
else
{
printf("0");
}
}
printf("\n");
//printf("%d\t%d\t%d\t%d\n", x1, x2, x3, x4);
printf("\n1 = Number of entered numbers within 1 standrad deviation from the mean");
printf("\n2 = Number of entered numbers between 1 and 2 standrad deviations from the mean");
printf("\n3 = Number of entered numbers betweenwith 2 and 3 standrad deviations from the mean");
printf("\n4 = Number of entered numbers more than 3 standrad deviations from the mean\n");
}
///////////////////////////////////////////////
int main(void)
{
float a[20],limit,S=0,S2=0,Mu=0,Mu2=0,Sig=0;
int i=0;
char line[50];
char filename[20];
system("clear");
FILE*file;
//Opens the specifed file for reading
printf("Please enter the file name you wish to open : ");
scanf("%s",filename);
file = fopen(filename ,"r");// Open for reading
//File reading error check
if (file==NULL) printf("fopen failed!!\n\n");
//Reads each line until white space
while (fgets(line,20,file)!=NULL)
{
sscanf(line,"%f", &limit);
a[i] = limit;
i++;
}
//Mathematical calculations
for(i=0; i<limit; i++)
{
S = S + a[i];
S2 = S2 + pow(a[i],2);
Mu = S/limit;
Mu2 = S2/limit;
Sig = sqrt(Mu2-pow(Mu,2));
}
//Displaying of Calculation answers
printf("\n Sum = %g", S);
printf("\n Sum of the Squares = %g", S2);
printf("\n Mean = %g", Mu);
printf("\n Mean of the Squares = %g", Mu2);
printf("\n Standard Deviation = %g", Sig);
printf("\n\n");
//Calling table function for difference from the mean and quantity
table(a,Mu,Sig,limit, 3, 0);
printf("1 = Difference from the mean\n");
printf("2 = Quantity\n");
printf("\n");
//Calling table function for standard deviation from the mean
table(a,Mu,Sig,limit, 3, 1);
return 0;
}
My input file (which causes corruption when printing) is:
1
2
32
4
55
66.789
7
8
9
10
11
12
31
40