2013-12-12 6 views
0

다음은 제 코드입니다. 그것은 사용자 벌금에서 정보를 취할 것입니다,하지만 그것은 prims를 호출하지 않습니다! (호출하기 전에 문을 출력하지 않습니다.). 문제는,이 main()은 단순히 primls 대신에 kruskals를 사용하여이 문제에 대한 복사와 붙여 넣기입니다. 메인은 변경되지 않고 잘 작동 했었지만, 유일한 차이점은 prims()가 현재 존재한다는 것입니다 . 나는 왜 프로그램이 멈추는 지 알 수 없다. (아무 것도하지 않는다. 깜박이는 커서는 없다.) 무슨 일이야?프로그램이 멈추고 아무 이유없이 아무 일도하지 않습니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#define MAX 50 

typedef struct graph{ 
    int vertices; 
    int edges; 
    int vertex[MAX]; 
    int edge[MAX][4]; /*[i][0]=i (edge ref) [i][1]=vertex1 [i][2]=vertex2 [i][3]=weight*/ 
} Graph; 

void prims(Graph graph); 

int main() { 

    Graph* graph=malloc(sizeof *graph); 
    printf("Please enter the number of vertices in your graph: "); 
    scanf("%i", &graph->vertices); 
    printf("\nPlease enter the number of edges in your graph: "); 
    scanf("%i", &graph->edges); 

    for (int i=0; i<graph->edges; ++i) { 
     graph->edge[i][0]=i; 
    ensure_valid_input: 
     printf("\nPlease enter the vertices connected by edge %i, and its weight: ",i+1); 
     scanf("%i %i %i", &graph->edge[i][1], &graph->edge[i][2], &graph->edge[i][3]); 
     if (graph->edge[i][1]>graph->vertices || graph->edge[i][2]>graph->vertices || graph->edge[i][1] <= 0 || graph->edge[i][2] <= 0) { 
      printf("\nERROR: One of these vertices is invalid; ensure they are both in range 1 - %d\n", graph->vertices); 
      goto ensure_valid_input; 
     } 
    } 

    printf("Try to call the function?"); 

    prims(*graph); 

    /*Print result to screen*/ 

    /*Print result to file*/ 

    return 0; 
} 


void prims(Graph graph){ 

    printf("Function called..."); 

    /*Initialise sets and test-values*/ 

    int edges_ordered[graph.edges]; 
    int used_vertices[graph.vertices]; 
    int used_edges[graph.vertices-1]; 
    int least_avail_edge; 
    int least_edge_reset; 
    int existing_entry; 
    int done=1; 
    int vertex_present; 

    for (int i=0; i<graph.vertices; ++i) { 
     if (i=0) { 
      used_vertices[i]=0; 
     } 
     else { 
      used_vertices[i]=-1; 
     } 

    } 

    /*Order the edges*/ 

    for (int i=0; i<graph.edges; ++i) { 
     least_avail_edge = least_edge_reset; 
     existing_entry = 1; 
     for (int j=0; j<graph.edges; ++j) { 
      if (graph.edge[j][3]<=graph.edge[least_avail_edge][3]) { 
       for (int k=0; k<graph.edges; ++k) { 
        if (edges_ordered[k]==graph.edge[j][0]) { 
         existing_entry=0; 
        } 
       } 
       if (existing_entry==1) { 
        least_avail_edge=j; 
       } 
      } 
     } 
     edges_ordered[i]=least_avail_edge; 
    } 

    //Diagnotstic Print 

    for (int i=0; i<graph.edges; ++i) { 
     printf("\n%d) Edge %d, Weight %d\n)", i+1, edges_ordered[i]+1, graph.edge[i][3]); 
    } 



    /*Continually add next appropriate edge to tree until spanning*/ 

    while (done!=0) { 



     /*Test to see if all vertices are in the tree yet*/ 
     done=0; 
     for (int i=0; i<graph.vertices; ++i) { 
      vertex_present=1; 
      for (int j=0; j<graph.vertices; ++j) { 
       if (graph.vertex[i]==used_vertices[j]) { 
        vertex_present=0; 
       } 
      } 
      if (vertex_present==1) { 
       /*Vertex is missing from tree -- not done!*/ 
       done=1; 
       break; 
      } 
     } 
    } 
} 
+0

이 질문은 올바른 질문이 아니지만 디버거에 질문하고 (사용법을 배워야합니다.) 귀하의 프로그램을 추적하고 그것이 무엇을 참조하십시오. –

답변

3

이 코드에 많은 문제가있다,하지만 난 범인이 prims() 함수 내에서 의심 :

if (i=0) { 

당신이 ==을 의미 생각합니다. 루프를 종료 할 때마다 i을 0으로 설정하면 프로그램이 멈추지 않습니다.

다른 문제는 least_edge_resetmalloc의 반환 값이 캐스팅되지 초기화되지 않은 것입니다 (당신이 오류가 발생하거나 이에 대한 경고 것입니다 여부를 C/C++ 버전 및 컴파일러 설정에 따라 다름), 그리고 sizeof *graph이 어색 점이다. 또한 최대 엣지 수 등을 초과하는 것에 대한 보호는 없지만 물어 보는 것이 아니기 때문에 거기에서 멈출 것입니다.

나는 당신의 print 문이 실행 의심하지만 \n이 없기 때문에, 그것은 버퍼링되고 바로 화면에 표시되지 및 prims()이 무한 루프에 갇혀 있기 때문에 표시되지 없구요.

+0

일부 프로그래머는'if (0 == i)'를 쓰는 연습을 채택합니다. 왜냐하면'=='대신에'='를 쓰면 컴파일러에서 번호를 먼저 빼면 오류가 발생하기 때문입니다. (일반적인 실수!) – Kaganar

+2

또는 합리적인 품질의 컴파일러를 적절한 경고 설정과 함께 사용하는 다른 방법. –

+0

-Wall은 친구입니다. – Speed8ump

관련 문제