# Thread: Need help with a small issue...

1. ## 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");
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. 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. > (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. and system() wouldnt be a good idea for pausing the program, since it is system call. Why not use getchar function

ssharish2005

5. ## 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',,,

yoyo2004

6. Originally Posted by yoyo2004
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',,,