What is wrong with my fread()

#include<stdlib.h>
#include<stdio.h>
#include<math.h>

double stdDev(double avg, double x[])
{
    double stdDeviation;
    double diff;
    double Sqdiff;
    double sum=0;
    int i;
    for(i = 0; i<50; i++)
    {
            diff=x[i]-avg;
            Sqdiff=pow(diff,2);
            sum+=Sqdiff;
            
    }
    
    stdDeviation=sum/avg;
    return sqrt(stdDeviation);
    
}
    
    
double maximum(double x[])
{
int i;
double max;
max=0;
for( i = 0; i<50; i++)
{

if(max< x[i])
max=x[i];
}
return max;
}

double average( double x[])
{
     double sum = 0;
     int i;
     for( i= 0; i<50; i++)
     sum+=x[i];
     return sum/100;
}

double minimum(double x[])
{
int i;
double min;
min=105;
for( i = 0; i<50; i++)
{
if(min > x[i])
min=x[i];
}
return min;
}


void Grades (int *Acounter,int *Bcounter,int *Ccounter,int *Dcounter,int *Ecounter, double x[])
{
         
int A=0,B=0,C=0,D=0,E=0;
int i;
for(i = 0; i<100 ; i++)
{
if(x[i] >= 90 && x[i] <= 100)
A++;
else if (x[i] >= 80 && x[i] <= 90)
B++;
else if (x[i] >= 70 && x[i] <= 80)
C++;
else if (x[i] >= 60 && x[i] <= 70)
D++;
else if (x[i]<60)
E++;
}
*Acounter = A;
*Bcounter = B;
*Ccounter = C;
*Dcounter = D;
*Ecounter = E;
}


int mode(double x[])
{
    int current;
    int mode[50];
    int i;
    for( i =0;i<50; i++)
    {
         current=x[i];
         current--;
         mode[current]++;
         } 
    int curr_max;
    curr_max=-99;
     for( i = 0; i<50; i++)
{

if(curr_max < mode[i])
curr_max=x[i];
}
return curr_max;

}

int main ()
{

        FILE *fp;
        fp=fopen("random.txt","r");
        if(!fp)
        {
        printf("Unable to open file!");
			system("pause");
		}
        double x[50];
        int max,min;
        double avg;
        int Acounter=0,Bcounter=0,Ccounter=0,Dcounter=0,Ecounter=0;
        int i;
        for(i =0; i<50; i++)
        {
                fread(&x[i],sizeof(double),1,fp);
                
        }
        
        
        max=maximum(x);
        min=minimum(x);
        avg = average(x);
        double stdDeviation;
        stdDeviation=stdDev(avg,x);
        for(i =0; i<50; i++)
        {
                printf("This is %d \n",x[i]);
                
        }
        printf("The avg is %d \n",avg);
        printf("The max is %d \n",max);
        printf("The min is %d \n",min);
        printf("The stdDev is %d \n",stdDeviation);
        
        //fclose("fp");
        system("pause");
        return 0;
         
}

i keep getting stuff like

This is 941635894
This is 540292384
This is 540287028
This is 859054130
This is 540352562
This is 808525874
This is 540554272
This is 876093494
This is 958413106
This is 857749557
This is 958411808
This is 540030752
This is 926228529
This is 540620832
This is 825499696
This is 825303091
This is 924856884
This is 891304243
This is -134215136
This is 16
This is 4133464
This is 8
This is 0
This is 4133280
This is 4133472
This is 7846544
This is 0
This is 0
This is 16842755
This is 16
This is 2005475021
This is -2
This is 2005218418
This is 4133472
This is 0
This is 4133464
This is 1993316557
This is 0
This is -1863876850
This is 0
This is -436195834
This is 1993321977
This is 2686672
This is 2686916
This is -436201242
This is 1993316570
This is 4133472
This is 2686776
This is 0
This is 2686832
The avg is -436195834
The max is -2147483648
The min is 0
The stdDev is -436195834

pls help


and this is the text

69 87 44 54 98 84 4 85 32 43 32 2 5 66 82 100 87 48 29 16 84 35 29 9 62 58 34 59 4 91 11 70 25 51 57 2 4 89 73 80 41 63 3 11 72 42 7 84 39 50 69

Recommended Answers

All 3 Replies

Can you put the print for loop just after you read the content from the file. Post the output here

The line

fp=fopen("random.txt","r");

makes me believe that the file contains textual representation of data. It must be converted to doubles via a proper format specifier of scanf, or via strtod, etc.
fread doesn't do any conversion. It just reads bytes.

What does %d mean in a printf() statement?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.