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 :
Any suggestion???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("%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; }



LinkBack URL
About LinkBacks



