Code:
this is my school project i think i have almost completed it .. i have to input data from external file and write it in other out put file... i have to fine the max and min of the given data in the input file...
the program is getting crashed.. please help
#include <stdio.h>
int main ()
{
int i,a[10],val,n=0,max,min;
FILE *fi,*fo;
if((fi=fopen("indata.txt","r"))==NULL)
{
printf("Error in opening the file!!!\n");
return 1;
}
else
{
while(fscanf(fi,"%f",&val))
{
a[n]=val;
n=n+1;
}
max = a[0];
min = a[0];
for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
if((fo=fopen("result.txt","w"))==NULL)
{
printf("Error in opening the file!!!\n");
return 1;
}
else
{
fprintf (fo,"Maximum element in an array : %d\n", max);
fprintf (fo,"Minimum element in an array : %d\n", min);
}
fclose(fo);
fclose(fi);
return 0;
}
}