Help with something in C Programming.

Started by markbilly, Wed 02/12/2009 22:40:18

Previous topic - Next topic

markbilly

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.

Code: ags


        while (num_on_line <= upper_lim) 
        {
                fscanf(input, "%f", &num_on_line);
		num_lines++;
	}



I've also tried this:

Code: ags


        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 ?
 

Calin Leafshade

#1
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

markbilly

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.
 

Calin Leafshade

lol oh.. :p

I'm a physicist too... a physicist of lurv

..do you ever get called a physician?

markbilly

Yes. My brother (a social scientist) called me that once. He doesn't any more! ;)

Any ideas on the program?
 

bicilotti

#5
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?


Crimson Wizard

#6
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.

markbilly

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.
 

Calin Leafshade

yeah your using the float formatting option for an int

%i

markbilly

The list of numbers are like this.

So they have to be scanned as floats, surely?

 

Calin Leafshade

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

bicilotti

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

markbilly

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...
 

Crimson Wizard

Can you please show a full function here?
The problem can be anywhere, maybe it is not in the lines you already posted.

Calin Leafshade

i would guess your initial condition is coming out as false and thus the loop never gets run.

Crimson Wizard

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.  :-\

markbilly

#16
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.

Code: ags


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

 

Crimson Wizard

#17
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.

markbilly

#18
That just returns area as zero all the time, now.

Getting output:

number of lines: 2
answer to integral: 0.000000
 

Crimson Wizard

Well, I ran you program with argument = 1.1 and got:

number of lines: 14007
nswer to integral: 677823296.000000


SMF spam blocked by CleanTalk