Adventure Game Studio

Community => General Discussion => Topic started by: markbilly on Wed 02/12/2009 22:40:18

Title: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 22:40:18
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 ?
Title: Re: Help with something in C Programming.
Post by: Calin Leafshade on Wed 02/12/2009 22:42:39
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
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 22:47:08
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.
Title: Re: Help with something in C Programming.
Post by: Calin Leafshade on Wed 02/12/2009 22:49:42
lol oh.. :p

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

..do you ever get called a physician?
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 22:54:55
Yes. My brother (a social scientist) called me that once. He doesn't any more! ;)

Any ideas on the program?
Title: Re: Help with something in C Programming.
Post by: on Wed 02/12/2009 23:15:12
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?

Title: Re: Help with something in C Programming.
Post by: Crimson Wizard on Wed 02/12/2009 23:21:26
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.
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 23:23:06
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.
Title: Re: Help with something in C Programming.
Post by: Calin Leafshade on Wed 02/12/2009 23:24:03
yeah your using the float formatting option for an int

%i
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 23:27:41
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?

Title: Re: Help with something in C Programming.
Post by: Calin Leafshade on Wed 02/12/2009 23:28:36
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);
Title: Re: Help with something in C Programming.
Post by: on Wed 02/12/2009 23:30:29
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).
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 23:32:27
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...
Title: Re: Help with something in C Programming.
Post by: Crimson Wizard on Wed 02/12/2009 23:44:26
Can you please show a full function here?
The problem can be anywhere, maybe it is not in the lines you already posted.
Title: Re: Help with something in C Programming.
Post by: Calin Leafshade on Wed 02/12/2009 23:45:07
i would guess your initial condition is coming out as false and thus the loop never gets run.
Title: Re: Help with something in C Programming.
Post by: Crimson Wizard on Wed 02/12/2009 23:46:35
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.  :-\
Title: Re: Help with something in C Programming.
Post by: markbilly on Wed 02/12/2009 23:59:26
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);
}

Title: Re: Help with something in C Programming.
Post by: Crimson Wizard on Thu 03/12/2009 00:15:57
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.
Title: Re: Help with something in C Programming.
Post by: markbilly on Thu 03/12/2009 00:17:59
That just returns area as zero all the time, now.

Getting output:

number of lines: 2
answer to integral: 0.000000
Title: Re: Help with something in C Programming.
Post by: Crimson Wizard on Thu 03/12/2009 00:25:50
Well, I ran you program with argument = 1.1 and got:

number of lines: 14007
nswer to integral: 677823296.000000

Title: Re: Help with something in C Programming.
Post by: markbilly on Thu 03/12/2009 00:31:31
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.
Title: Re: Help with something in C Programming.
Post by: markbilly on Thu 03/12/2009 01:07:32
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! :)