comp.lang.c
[Top] [All Lists]

Re: need help with arrays

Subject: Re: need help with arrays
From: pereges
Date: Tue, 29 Apr 2008 10:20:23 -0700 PDT
Newsgroups: comp.lang.c

On Apr 29, 9:50 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
<snip>

> Better use malloc.  There is no advantage in zeroing (arithmetically)
> the pointers.

How is malloc any different ? Both will achieve same purpose ?

<snip>
> Also, watch our for you <=.  Take a 3 x 4 rectangle with 48 rays and
> incr = 0.5.  If you go <= you get both an extra row and ne more ray
> per row:
>
>   X--X--X--X--X--X--X--X--X
>   |     |     |     |     |
>   X  X  X  X  X  X  X  X  X
>   |     |     |     |     |
>   X--X--X--X--X--X--X--X--X
>   |     |     |     |     |
>   X  X  X  X  X  X  X  X  X
>   |     |     |     |     |
>   X--X--X--X--X--X--X--X--X
>   |     |     |     |     |
>   X  X  X  X  X  X  X  X  X
>   |     |     |     |     |
>   X--X--X--X--X--X--X--X--X
>
> That is 63 rays, not 48!
>


I have changed my approach a little bit for generating points on the
grid.

/*****************************************************************/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
        long int numpoints;
        printf("Enter num of points\n");
        scanf("%d", &numpoints);
        long int numpointsx = sqrt((double)numpoints); //num of points
along x
        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;
        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)
       {
            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
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.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy