I have a file with a big long list of numbers in.
I want to count the number of lines in the file up to the number closest (rounded down) to one inputted by the user.
So say I have: "upper_lim" the number entered by the user.
"num_lines" the number of lines counted.
"num_on_line" the number on the file of the file.
I've done this while() loop, but I'm getting a nonsense number.
while (num_on_line <= upper_lim)
{
fscanf(input, "%f", &num_on_line);
num_lines++;
}
I've also tried this:
while (!EOF)
{
fscanf(input, "%f", &num_on_line);
if ( num_on_line <= upper_lim )
{
num_lines++;
}
}
Help! Why am I getting a nonsense number like 2.07e-310 ?
Quote from: markbilly on Wed 02/12/2009 22:40:18
Help! Why am I getting a nonsense number like 2.07e-310 ?
Thats not a nonsense number :p its standard form.
it mean 2.07 * 10^310.. i.e a very big number. your loop is getting stuck or something
EDIT: no wait!
2.07 * 10^-310 a very SMALL number.. oops
I know what orders of magnitude are, I'm a Physicist! ;) I meant in terms of the program it's nonsense. I doubt many data files have a number of lines < 1.
lol oh.. :p
I'm a physicist too... a physicist of lurv
..do you ever get called a physician?
Yes. My brother (a social scientist) called me that once. He doesn't any more! ;)
Any ideas on the program?
Not sure. Maybe:
fscan reads formatted things. Are you using fprintf to write the data?
Also, what does exactly give you the 2.07 * 10^-310 output? (A printf I suppose) Sure to have put %d and not %f in the first argument?
Is num_lines an int?
As bicilotti sais, you maybe use wrong formatting option. %f is used for floating-point, for integer you should use %i or %d.
Yet, you did not explained where do you get this result exactly, so it is just a guess.
num_lines is an int
upper_lim and num_on_line are floats
a printf("number of lines: %f \n", num_lines); gives the nonsense number.
yeah your using the float formatting option for an int
%i
The list of numbers are like this. (http://www2.warwick.ac.uk/fac/sci/physics/teach/module_home/px270/assignments/px270prog3a.dat)
So they have to be scanned as floats, surely?
well perhaps, but the number of lines is still an integer
you cant have half a line..
printf("number of lines: %d \n", num_lines);
Quote from: markbilly on Wed 02/12/2009 23:23:06
a printf("number of lines: %f \n", num_lines); gives the nonsense number.
printf("number of lines: %d\n", num_lines); should work
Maybe you want to use
cout << to get screen output, so to forget about % formatting (though it's c++ lib., not c).
It doesn't matter whether I use %i, %f, %g, whatever, it still returns a value of zero. Whether that is by displaying it as 2.7e-310 or 0.000000 or just 0.
There is something messing up with the loop and I can't work out what...
Can you please show a full function here?
The problem can be anywhere, maybe it is not in the lines you already posted.
i would guess your initial condition is coming out as false and thus the loop never gets run.
For example, how EOF is initialized and where is it set after then?
Even if it is initialized properly, I don't see how it will change afterwards, thus from my point of view it may cause endless loop. :-\
This is the full code, I'm get the value of the area under a function between 0 and the "upper_lim" i.e. upper limit of integration. The data file contains x values in one column and corresponding f(x) values in another column.
The code is rubbish. I'm not 100% sure what I'm doing.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*define function*/
float area_intg(float *fnc_fx, float *fnc_dx, int lines);
int main(int argc, char* argv[])
{
/*define a float for user-inputed upper limit of integration*/
float user_input, upper_lim, answer;
float *value_x, *value_xmin1, *dx, *fx, *fx_xmin1;
/*Check the upper limit is enter via command-line correctly*/
if (argc != 2)
{
printf("Need to enter upper limit of integration \n");
exit(EXIT_FAILURE);
}
/*Assign the user's string input to the float "upper_lim"*/
user_input = sscanf(argv[1], "%f", &upper_lim);
/*upper_lim = atof(argv[1]);*/
/*Check upper_lim is valid*/
if (upper_lim == 0)
{
printf("Upper limit cannot be 0, this is the lower limit. \n");
exit(EXIT_FAILURE);
}
if (upper_lim >= 1.57)
{
printf("Upper limit cannot exceed pi/2. \n");
exit(EXIT_FAILURE);
}
/*open the file containing data*/
FILE *input;
const char file_input[] = "px270prog3a.dat";
input = fopen(file_input, "r");
/*Find "num_lines" using while loop*/
int num_lines = 0;
float num_on_line;
while (num_on_line <= upper_lim)
{
fscanf(input, "%f", &num_on_line);
num_lines++;
}
/*manually assign number of array arguments, using malloc*/
value_x = (float*) malloc(sizeof(float) * num_lines);
value_xmin1 = (float*) malloc(sizeof(float) * num_lines);
fx = (float*) malloc(sizeof(float) * num_lines);
dx = (float*) malloc(sizeof(float) * num_lines);
/*scan file and store data in arrays*/
int n = 0;
for (n = 0; n < num_lines; n++)
{
fscanf(input, "%f %f", &value_x[n], &fx[n]);
fscanf(input, "%f", &value_xmin1[n-1]);
dx[n] = value_x[n] - value_xmin1[n-1];
}
answer = area_intg(fx, dx, num_lines);
printf("number of lines: %i \nanswer to integral: %f \n", num_lines, answer);
}
float area_intg(float *fnc_fx, float *fnc_dx, int lines)
{
float area;
int j;
for (j = 1; j < (lines); j++)
{
area += fnc_fx[j-1] * fnc_dx[j];
}
return(area);
}
Two warnings:
1) num_on_line is used unitialized. Better write
float num_on_line =0.0; or something
2) in function area_intg variable area is used unitialized. Better write
float area = 0.0;
Otherwise you may get unexpected results. This depends on compiler you use; AFAIK Microsoft C compilers usually do not initialize variables with zeros.
By the way, since num_on_line is used in loop condition, it may make the error you have. So try to initialize it before loop and see what happen.
That just returns area as zero all the time, now.
Getting output:
number of lines: 2
answer to integral: 0.000000
Well, I ran you program with argument = 1.1 and got:
number of lines: 14007
nswer to integral: 677823296.000000
For 1.1 I get:
number of lines: 14007
answer to integral: 2.254410
the answer should be about 0.75, though. Also, for arguments less than 1 it goes funny and has the 2, 0.000000 output.
Not to worry. This was a uni assignment so I just looked up the amount of credit I get for it and decided it wasn't worth the effort, frankly. I've been pouring over it for hours. I just submitted what I had, so should still get some marks.
Thanks everyone for the help, though! :)