calculate coverage degree of node

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • latalui
    New Member
    • Sep 2008
    • 10

    calculate coverage degree of node

    i want to calulate coverage degree for a particular node having total number of nodes say n=10, in an xy coordinate having say size of x= y=500 and initial position of node is x = y= 0.5 and it should incremented by 1. Condition is that each node is capable of covering distance say d =5. i,ve tried but given below code is not working. pleaase somebody help me out.

    #include<stdio. h>
    #include<stdlib .h>
    #include<math.h >
    Struct node {
    float pos_x;
    float pos_y;
    };
    Struct node s[50];
    int main()
    {
    float d;
    float x = 0.5;
    float y=.05;
    float degree = 0.0;
    int i, n;
    printf(“enter the number of nodes”);
    scanf(“%d”, &n);
    for(i=0; i<n; i++)
    {
    S[i].pos_x = random(500);
    S[i].pos_y = random(500);
    }
    For(y=0.5 ; y<499.5; y++)
    {
    For(x=0.5 ;x<499.5; x++)
    {
    for(i=0; i<n; i++)
    {
    d = sqrt(pow((x-s[i].pos_x),2.0 )+ pow((y-s[i].pos_y),2.0));
    if(d<5)
    degree++;
    }
    degree = degree / 500.0;
    printf(“ The average coverage is %f”,degree);
    }
    }
    return(0);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Please put CODE tags around your sample code.

    Next, post only code that compiles (unless you have a syntax question).

    Next, this code y<499.5 can't work. The <, > , ==, etc operators don't work with floating point. Google floating point arithmetic and you will see what I mean.

    Next, 499.5 is a double. y is a float. Mixing float and double will cause compiler warnings about truncation and possible loss of data. Use double for everything unless you can write down on paper the technical reason why you can't.

    Next, put comments in your code. Often problems are solved when the comments and the code don't match.

    Next, indent your braces so this code is readable.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by weaknessforcats
      Next, this code y<499.5 can't work. The <, > , ==, etc operators don't work with floating point. Google floating point arithmetic and you will see what I mean.
      Actually, "<" and ">" are the only comparison operators that work properly with floating point. The problem is the way you're using them to control the number of iterations of a loop. That's a bad idea. The number of iterations could well be off-by-one from what you expect.

      Comment

      • latalui
        New Member
        • Sep 2008
        • 10

        #4
        #include<stdio. h>
        #include<stdlib .h>
        #include<math.h >
        Struct node {
        float pos_x;
        float pos_y;
        };
        Struct node s[50];
        int main()
        {
        float d;
        float x = 0.5;
        float y=.05;
        float degree = 0.0;
        int i, n;
        printf(“enter the number of nodes”);
        scanf(“%d”, &n);
        for(i=0; i<n; i++)
        {
        S[i].pos_x = random(500);
        S[i].pos_y = random(500);
        }
        for(y=0.5 ; y<499.5; y++) // for coordinate y axis varying from .5 to 500
        {
        for(x=0.5 ;x<499.5; x++) for coordinate y axis varying from .5 to 500

        {
        for(i=0; i<n; i++)
        {
        d = sqrt(pow((x-s[i].pos_x),2.0 )+ pow((y-s[i].pos_y),2.0));
        if(d<5)
        degree++;
        }
        degree = degree / 500.0;
        printf(“ The average coverage is %f”,degree);
        }
        }
        return(0);
        }

        Comment

        • latalui
          New Member
          • Sep 2008
          • 10

          #5
          #include<stdio. h>
          #include<stdlib .h>
          #include<math.h >
          Struct node {
          float pos_x;
          float pos_y;
          };
          Struct node s[50];
          int main()
          {
          float d;
          float x = 0.5;
          float y=.05;
          float degree = 0.0;
          int i, n;
          printf(“enter the number of nodes”);
          scanf(“%d”, &n);
          for(i=0; i<n; i++)
          {
          S[i].pos_x = random(500);
          S[i].pos_y = random(500);
          }
          for(y=0.5 ; y<500; y++) // for coordinate y axis varying from .5 to 500
          {
          for(x=0.5 ;x<500; x++) /* for coordinate x axis varying from .5 to 500

          {
          for(i=0; i<n; i++)
          {
          d = sqrt(pow((x-s[i].pos_x),2.0 )+ pow((y-s[i].pos_y),2.0)); /* to calculate distance between nodes using sqr (x1 - x2) + sqr(y1-y2).*/
          if(d<5) /*if distance between them is under sensing range d we increment the degree of coverage and then print coverge degree of that particular node*/
          degree++;
          }
          degree = degree / 500.0;
          printf(“ The average coverage is %f”,degree);
          }
          }
          return(0);
          }

          Comment

          Working...