2017-07-27 46 views
-1
int main() 
{ 
    FILE *wg = NULL; 
    wg = fopen("wangdata.txt","w"); 
    if(wg == NULL) 
    { 
     printf("Error in opening file wg!\n"); 
    } 

    int vertices = 100, edges = 400, i; // 20,50:(100,50) 
    int strings = 160; 
    int v1, v2,j; 
    double t=0.0; 
    double dt=0.1; 
    double b[strings]; 
    double x[vertices], x1[vertices]; 
    double x2[vertices]; 
    unifRand(x,x1,x2); 

    struct Edge * adjacencyList[vertices + 1]; 
    // Size is made (vertices + 1) to use the 
    // array as 1-indexed, for simplicity 

    // initialize array: 
    for (i = 0; i <= vertices; ++i) { 
     adjacencyList[i] = NULL; 
    } 

    for (i = 0; i <= edges; ++i) { 
     //scanf(%d%d", &v1, &v2); 
     v1 = rand()%100; 
     v2 = rand()%100; 

     // Adding edge v1 --> v2 
     // Add edge from v1 --> v2 
     if(v1 != v2) 
     adjacencyList[v1] = addEdge(adjacencyList[v1], v2); 

     // Adding edge v2 --> v1 
     // Remove this if you want a Directed Graph 
     adjacencyList[v2] = addEdge(adjacencyList[v2], v1); 
    } 

    // Printing Adjacency List 
    printf("\nAdjacency List -\n\n"); 
    for(j=0; j<strings; j++){ 

     for (i = 0; i <= vertices; ++i) { 
      printf("adjacencyList[%d] -> ", i); 
      struct Edge * traverse = adjacencyList[i]; 
      while (traverse != NULL) 
      { 
       b[j] = j/vertices; 
       fprintf(wg,"%d %d \t\t%0.3lf\t\t\t%0.1lf\t%0.6lf\t\n", i, traverse->vertex, (-log(1-x[i])*(traverse->vertex)),b[j],x[i]); 
       printf("%d -> ", traverse->vertex); 
       traverse = traverse->next; 
      } 
      printf("NULL\n"); 
     } 
    } 

    return 0; 
    fclose(wg); 
    wg = NULL; 
} 

上述for循環中的主要功能,僅在輸出文件中打印01,作爲b[j]。在打印j時,它會打印所有j的值,但該循環不適用於b[j]。我不知道哪裏出了問題?我將b[j]定義爲總頂點歸一化的字符串數(圖論中的網絡大小)。爲什麼for循環對特定值不起作用?

+0

頂點在哪裏申報?請添加更多你的代碼。 –

+5

你確定*關於循環條件'我<=頂點'? –

+0

你是不是指'b [j] = j/i;'? – CinCout

回答

1

您將int除以int。這將產生一個int值。由於j將始終低於vertices,你會得到0

嘗試

b[j] = (double)j/(double)vertices; 
+0

未能注意到微不足道的問題。它現在有效。謝謝 – Kevin

1

j從0到159,由100向零除由於整數除法輪,這給0或1

如果你想有一個非舍double結果,投下數的一個或兩個double分裂之前:

b[j] = j/(double) vertices;