#include #include #include "adjacent.h" void read_num_nodes(FILE *fptr, int *num_nodes){ // read first value in file if((fscanf(fptr, "%d", num_nodes)) == EOF){ return; } } void read_adjacent_nodes(FILE *fptr, int num_nodes, int **adjacent){ // declare variables int a, b, c = 0; // store values int i = 0; // incrementor // ensure the fptr is at the beginning rewind(fptr); // allocate memory and move fptr to correct spot fscanf(fptr, "%d %d", &a, &b); // move past line 1 //printf("%d %d\n", a, b); // for testing for(i = 0; i < num_nodes; i++){ adjacent[i] = calloc(num_nodes, sizeof(int)); // init to 0 fscanf(fptr, "%d\t%d\t%d", &a, &b, &c); // move through //printf("%d\t%d\t%d\n", a, b, c); // for testing } // scan relevent values while(fscanf(fptr, "%d %d", &a, &b) != EOF){ //printf("%d %d\n", a, b); // for testing adjacent[a][b] = 1; adjacent[b][a] = 1; } } int main (int argc, char **argv){ int num_nodes = 0; // open file for reading FILE *fptr = fopen(argv[1], "r"); if (fptr == NULL){ // check for error printf("File name error.\n"); return EXIT_FAILURE; } read_num_nodes(fptr, &num_nodes); int **adjacent = malloc(num_nodes * sizeof(int *)); read_adjacent_nodes(fptr, num_nodes, adjacent); // print values in proper format int i = 0; int j = 0; for(i = 0; i < num_nodes; i++){ printf("%d:", i); for(j = 0; j < num_nodes; j++){ if(adjacent[i][j] > 0){ printf(" %d", j); } } printf("\n"); } // clean up for(i = 0; i < num_nodes; i++){ free(adjacent[i]); } free(adjacent); fclose(fptr); return EXIT_SUCCESS; }