|
|
On Tue, 29 Apr 2008 10:20:23 -0700 (PDT), pereges <Broli00@xxxxxxxxx>
wrote:
snip 30+ obsolete lines
>I have changed my approach a little bit for generating points on the
>grid.
Then please don't quote irrelevant material.
>
>/*****************************************************************/
>
>#include <stdio.h>
>#include <math.h>
>#include <stdlib.h>
>
>int main(void)
>{
> long int numpoints;
> printf("Enter num of points\n");
> scanf("%d", &numpoints);
This invokes undefined behavior. The %d promises scanf that the
corresponding argument will be an int*. It is obviously a long int*.
The fact that it appears to work on your system is just bad luck.
> long int numpointsx = sqrt((double)numpoints); //num of points
>along x
The cast serves no purpose.
If you want people to test your code, then this is one reason why you
should not use // style comments.
> long int numpointsy = sqrt((double)numpoints); // num of
>points along y
> numpoints = numpointsx * numpointsy; // total no of points for
>which i will allocate memory
> int i = 0;
Unless you have C99, declarations and definitions need to precede
executable statements.
> typedef struct point //temproray ds for point (just experimenting)
> {
> float x, y;
>
> } point;
> point *pointinarray;
>
> pointinarray = calloc(sizeof(point), numpoints); //new array with
>numpoints
> float xlength, ylength; //length and breadth
> xlength = ylength = 20;
> float xmin, ymin;
> xmin = ymin = -10;
> float xmax, ymax;
> xmax = ymax = 10;
> float xsize = xlength / numpointsx; //calculating increments
>along x and y
> float ysize = ylength / numpointsy;
>
> for (int yi = 0; yi < numpointsy; ++yi)
Defining a variable this way is an extension.
> {
> for (int xi = 0; xi < numpointsx; ++xi)
> {
> pointinarray[i].x = xmin + xi * xsize;
> pointinarray[i].y = ymin + yi * ysize;
> ++i;
> }
> }
>
> printf("i: %d xmax: %f ymax: %f\n",i, pointinarray[i-1].x,
>pointinarray[i-1].y);
> return 0;
>
>}
>
>/
>*****************************************************************************************/
>
>
>With this approach there seems to be no problem except 1.
>
>I checked out the last entry of pointsinarray[] which should
>correspond to xmax and ymax (10 and 10 respectively) and the values i
>get are:
>
>9.83509
What value did you enter for numpoints?
>9.83509
>
>You can verify by running the above program yourself and checking o/p.
>
>Also, I noticed that when I entered 14000 as number of rays, The total
>number of rays actually taken into consideration for calculation of
>grid were only 13924. This problem arises when number of rays is not a
>perfect square.
>i believe problems are occuring because numpointsx and numpointsy are
>getting truncated to smaller values. For eg. in c, the integer square
>root of 5 is 2. I think if I can tweak it to some higher integer
>value, problem will be solved. I believe there is a function in math.h
>which does that.
If you use an integer higher that the sqrt you will have the same
problem but in the opposite direction. WHY are you converting the
sqrt to integer? The only way to get a square grid with the number of
points you specify is to specify a perfect square.
Remove del for email
|
|