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

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));

}

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

3
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Ancient Dragon

You define, on line 123

char line[50];

And then use fgets on line 139

fgets(line,20,file)

Why?

In function ‘count’:
111: warning: control reaches end of non-void function
In function ‘table’:
49: warning: control reaches end of non-void function

Both functions, table and count, expect return values of float.

Edited by gerard4143: n/a

Ok I made a mistake it should be:

``fgets(line,50,file)``

and I do not get those errors when I compile the code.

Warnings not errors and you should be getting them.

Ok then could you give me some help as to how I can correct those errors?

line 147: why are you using variable limit in that loop? The value of limit could be any random number because its used in the previous loop as file input.

>>Ok then could you give me some help as to how I can correct those errors?
Make the two functions return a value. Or -- if no value is to be returned then declare the functions to return void.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.