Thread: Need help with a small issue...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Need help with a small issue...

    Hello all,,
    Im trying to make a program to calculate the degree of a node. Anyway every thing's seems working perfectly Except finding the average degree.
    Every time i got 0 . I don't know why...

    here is the code :

    Code:
    /**********************************
    Calculating the Graph's Degree
    
    Programme file: 
    Aothor: yoyo
    Date: 
    **********************************/
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     typedef struct node{
           int xCoodinate;
           int yCoodinate;
           int id;
           //bool neighbour[numOfnodes];
           int numOfdegree;
           }NODE;
    main()
    {
    
          /*declare variables*/
          int numOfnodes,option,nodeNo,avgDegree,totalDegree=0,min,max;
          float distance;
          float xMax;
          float yMax;
          unsigned int iseed = (unsigned int)time(NULL);
          srand (iseed);
         
         /*input the N(num of nodes)*/
          printf("Enter the Maximum Number of Nodes: ");
          scanf("&#37;d", &numOfnodes);
          NODE node[numOfnodes];
          
          /*input xMax and yMax*/
          printf("Enter Maximum dimension of an area(Xmax followed by Ymax) :");
          scanf("%f %f", &xMax,&yMax);
          int i,j;
          for(i=0;i<numOfnodes;i++){
          node[i].id=i;
          node[i].xCoodinate=(1 + (int)(( xMax * rand() )/ ( RAND_MAX + 1.0 ) ));
          node[i].yCoodinate=(1 + (int)(( yMax * rand() )/ ( RAND_MAX + 1.0 ) ));
          printf("Node %d Coodinates (%d,%d)\n",(i+1),node[i].xCoodinate,node[i].yCoodinate);
          }
          printf("Enter the Distance :");
          scanf("%f", &distance);
          for(i=0;i<numOfnodes;i++){
          for(j=0;j<numOfnodes;j++){
          if((j!=i)&&(sqrt(pow((node[i].xCoodinate-node[j].xCoodinate),2)+pow((node[i].yCoodinate-node[j].yCoodinate),2))<distance)) 
          {//node[i].neighbour[j]=true;
          node[i].numOfdegree++;
          }
          else 
          {
          node[i].numOfdegree=0;
          }
          }
          totalDegree+=node[i].numOfdegree;
                                        
          }
          
          
          
          
     
          /*display result*/
     do{
          system("cls");
          printf("\t\tAVAILABLE OPTIONS:\n\n");
          printf("\t\t---------------------\n\n");
          printf("\t\t1.Find the Average Degree\n");
          printf("\t\t2.Find Degree of Specific Node\n");
          printf("\t\t3.Minimum Degree\n");
          printf("\t\t4.Maximum Degree\n");
          printf("\t\t5.Exit\n");
          printf("\n\n");
          printf("\t\tEnter Your Option: ");
          scanf("%d", &option);
          
          switch(option){
          case 1:
               avgDegree=(totalDegree/numOfnodes);
               printf("Average Degree: %d\n",avgDegree);
               system("pause");
               break;
          case 2:
          system("cls");
          printf("\t\tAVAILABLE OPTIONS:\n\n");
          printf("\t\t------------------------\n\n");
               for(i=1;i<=numOfnodes;i++){
               printf("\t\tNODE %d:\n",i );
               }
               printf("\n\t\tEnter the No of the Node: ");
               scanf("%d",&nodeNo);
               printf("DEGREE: %d\n",(node[nodeNo-1].numOfdegree));
               system("pause");
               break;
          case 3:
               min=(numOfnodes+1);
               for(i=0;i<numOfnodes;i++){
               if(node[i].numOfdegree<min){min=node[i].numOfdegree;}
               }
               printf("Minimum Degree: %d\n",min);
               system("pause");
               break;
          case 4:
               max=0;
               for(i=0;i<numOfnodes;i++){
               if(node[i].numOfdegree>max){max=node[i].numOfdegree;}
               }
               printf("Maximum Degree: %d\n",max);
               system("pause");
               break;
          case 5:
               exit(1);
               break;
          default:
                  printf("\a\t\tOption unavailable");
          
          }
     }while(option<6);
          
          system("PAUSE");
    	  return 0;
    }
    Any suggestion???

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. You need to fix your indentation - it's all over the shop.
    Second, this line is far too long:
    Code:
    if((j!=i)&&(sqrt(pow((node[i].xCoodinate-node[j].xCoodinate),2)+pow((node[i].yCoodinate-node[j].yCoodinate),2))<distance))
    (And by the way, if you calculate the difference as a separate result, then square it using x * x, the calculation will be noticably fastar than the exponential expression used in pow().]

    3. main is int main() or int main(int argc, char **argv)


    4. Are you sure that the degree will be noticably larger than numberOfnodes? Otherwise, I would expect the integer calculation to be 1 or zero, depending on whcih number is greater.

    5. x and yCoordinate is missing the "r" in Coordinate..

    --
    Mats

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > (And by the way, if you calculate the difference as a separate result, then square it using x * x, the calculation will be noticeably
    > faster than the exponential expression used in pow().]

    Also, get rid of sqrt() completely by comparing the distance squared, to distance*distance.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    and system() wouldnt be a good idea for pausing the program, since it is system call. Why not use getchar function

    ssharish2005

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Thank's guys..

    I have done all what u have said ,and now am getting better results ..
    But i don't know why i have to get rid of sqrt().....And Matsp i don't know what do u mean by missing 'r',,,
    Thank's again for ur reply...


    yoyo2004

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by yoyo2004 View Post
    I have done all what u have said ,and now am getting better results ..
    But i don't know why i have to get rid of sqrt().....And Matsp i don't know what do u mean by missing 'r',,,
    Thank's again for ur reply...


    yoyo2004
    * You spelt "Coordinate" wrong, (without an 'r').
    * sqrt() is slow (in comparison to other things), robatino was merely suggesting an optimization - a smart one at that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Logic Issue
    By Flakster in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2006, 02:40 PM
  2. A small issue.
    By Overwhelm in forum C++ Programming
    Replies: 35
    Last Post: 01-18-2005, 05:17 PM
  3. Newb With Small Pointer Issue
    By G-Prime in forum C Programming
    Replies: 7
    Last Post: 09-06-2004, 04:09 PM
  4. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  5. Small issue with system()
    By Metal Man in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2003, 01:33 PM