I think that is my problem but I cannot seem to find it. The program is to take a file in that contains a cost matrix, and print out a table for the shortest path. I used a second array to store the three columns of visited, cost, and previous node(row in my case). I've given up on using multiple files / methods for now focusing on the actual task.
Code:#include "prog5.h" int main(int argc, char *argv[]){ int size, i, j, k, matrix_length, prims_length, row, column; int *matrix, *prims; FILE *infile; infile = fopen("prog5.in", "r"); fscanf(infile, "%d", &size); matrix = (int *)malloc(size*size*sizeof(int)); /*Create an array of integers large enough to hold the matrix*/ prims = (int *)calloc(size*3,sizeof(int)); /*Create an array of integers large enough to hold Prim's 3 column chart and sets initial to 0*/ matrix_length = size*size; /*gets the length of the matrix array*/ prims_length = size*3; /*gets the length of the matrix array*/ while(!feof(infile)){ /*while End of File is not true*/ for(i=0; i<matrix_length; i++){ fscanf(infile, "%d", &matrix[i]); /*fills the matrix array*/ }/*for(i=0; i<length; i++){...*/ }/*while(!feof(infile)){...*/ for(i=0; i<prims_length; i++){ prims[i] = 0; } for(j=0; j<matrix_length; j++){ printf("%d ", matrix[j]); if(j && j%size == 0) printf("\n"); } printf("\n"); for(j=0; j<prims_length; j=j+3){ printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]); }/*for(j=0; j<prim_length; j=j+3){...*/ printf("\n"); fclose(infile); row = 0; for(i=0; i < size; i++){ prims[3*row] = 1; /*set current row to visited*/ for(column = 0; column < size; column++){ if(matrix[(size*row)+column] != 0){ if(matrix[(size*row)+column] < prims[(3*row)+1]){ prims[(size*column)+1] = matrix[(size*row)+column]; prims[(size*column)+2] = row; }else if(prims[(3*row)+1] == 0){ prims[(size*column)+1] = matrix[(size*row)+column]; prims[(size*column)+2] = row; } /*sets cost and previous column in the prims array for the current column in the matrix if the cost is lower or zero*/ }/*if(matrix[size*row+column] != 0){...*/ }/*for(column = 1; column < size; column++){...*/ for(j=0; j<size; j++){ if(prims[3*j] == 0){ /*if the row isn't visited*/ for(k=0; k<size; k++){ if(k=j){ /*if the row is being compared to itself*/ /*do nothing*/ }else if(prims[3*k] == 0){ /*if the row being compared to has not been visited*/ if(prims[3*j+1]<prims[3*k+1]){ /*if the cost is less than the row being compared to*/ row = 3*j; }/*if(prims[3*j+1]<prims[3*k+1]){...*/ }/*else if(prims[3*k] == 0){...*/ }/*for(k=0; k<size; k++){...*/ }/* if(prims[3*j] == 0){...*/ }/*for*/ for(j=0; j<prims_length; j=j+3){ printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]); }/*for(j=0; j<prim_length; j=j+3){...*/ printf("\n"); }/*while(i=0; i<size; i++){...*/ free(matrix); free(prims); return 0; }/*int main(int argc, char *argv[]){...*/



LinkBack URL
About LinkBacks


