Reading numbers with scientific notation
Hi, I need some help with reading a set of numbers with scientific notations. Basically what my program does is that it reads a set of values in a text file, find the maximum value and then print it out.
My program works with values without scientific notation but crashes when I try to read text files containing scientific notations. Any ideas what I'm doing wrong?
The program works with this sort of file:
Code:
0.5 304.528
1.5 873.17
2.5 1636.12
3.5 1947.69
4.5 1189.97
5.5 1273.69
6.5 1650.59
7.5 1211.33
8.5 1211.71
9.5 1760.09
But crashes when I try to read this file:
Code:
5.50E+00 4.47E-02
6.75E+00 5.37E-02
7.00E+01 3.35E-02
8.25E+01 3.68E-02
9.50E+00 3.63E-02
1.75E+01 3.70E-02
2.00E+01 3.23E-02
3.03E+00 5.25E-02
4.05E+00 5.28E-02
5.08E+01 5.05E-02
The crash looks like this: http://img288.imageshack.us/img288/4775/crushbg3.jpg
My complier is quincy and I'm only allowed to use C
Code:
#include <stdio.h>
#define N 100
double max(double x[], double n);
main()
{
int k=0;
double y[N], q;
FILE *data;
data = fopen("textfile2.txt", "r");
if (data == NULL)
{
printf("Error opening file. \n");
exit(1);
}
else
{
while (fscanf(data, "%lf", &y[k]) == 1)
{
k++;
}
q = k;
printf("Maximum value: %lf \n", max(y, q));
fclose(data);
}
return 0;
}
/* This function returns the maximum value. Maybe there's something wrong with the function? */
double max(double x[], double n)
{
int k;
double max_x;
max_x = x[9];
for (k = 1; k <= n - 1; k++)
{
if (x[k] > max_x)
max_x = x[k];
}
return max_x;
}
Any help would be much appreciated, thanks!