Ok this is going well. It appears to be running like it should but it's not printing what I expect it too. Here is both the main and the print functions. I'm going to look into my pointers, I'm fairly sure it has something to do with that. That maybe I'm printing a location rather than what's there..... Thanks!
Code:
#include "prog5.h"
int main(int argc, char *argv[]){
int size, i, matrix_length, prims_length, row, column, edge;
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)){...*/
fclose(infile);
row = column = 0;
for(i=0; i < size; i++){
prims[size*row+0] = 1; /*set current row to visited*/
edge = 1;
for(column = 1; column < size; column++){
if(matrix[size*row+column] != 0 &&
matrix[size*row+column] < prims[size*column+1]){
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*/
}/*if(matrix[size*row+column] != 0) && ...*/
if(matrix[size*row+column] < edge && matrix[size*row+column] != 0){
edge = matrix[size*row+column];
row = column;
/*compares edges in the current row setting the
next row to the column with the lowest edge greater
than 0*/
}/*if(matrix[size*row+column] < edge ...*/
}/*for(column = 1; column < size; column++){...*/
printprims(&prims_length, &prims);
}/*while(i=0; i<size; i++){...*/
free(matrix);
free(prims);
return 0;
}/*int main(int argc, char *argv[]){...*/
Code:
#include "prog5.h"
void printprims(int *prims_length, int *prims[]){
int j;
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");
}/*void printprims(int *prims_length, int *prims[]){...*/