Search code examples
cadjacency-matrix

How to make an adjacency matrix out of a list from a file in C


Hey I want to make an adjacency matrix out of the list from a file in c but I am not sure how to do it. This is my code so far:

#include <stdio.h>
#include <stdlib.h>

typedef struct value {
  int num;
  char start_vertex[250];
  char destination_vertex[250];
} value;

int main()
{
  const int nLines = 43; // number of lines in my text file
  FILE * fptr;
  value * valuesPtr = malloc(sizeof(value) * nLines);

  if (!valuesPtr) {
    puts("cannot allocate memory");
    return -1;
  }

  if((fptr = fopen("energy.txt", "r")) == NULL)
  {
    perror("Error opening file");
    return -1;
  }

  for(int i = 0; i < nLines; i++ )
  {
      if (fscanf(fptr, "%249s %249s %d",
                 valuesPtr[i].start_vertex,
                 valuesPtr[i].destination_vertex,
                 &valuesPtr[i].num) != 3) {
        printf("errored file line %d\n", i);
        break;
      }

      printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
             valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
  }
  free(valuesPtr);
  fclose(fptr);

  return 0;
}

The energy file has the following content in it

York    Hull    60
Leeds   Doncaster   -47
Liverpool   Nottingham  161
Manchester  Sheffield   61
Reading Oxford  -43
Oxford  Birmingham  103
Birmingham  Leicester   63
Liverpool   Blackpool   79
Carlisle    Newcastle   92
Nottingham  Birmingham  77
Leeds   York    39
Glasgow Edinburgh   74
Moffat  Carlisle    65
Doncaster   Hull    76
Northampton Birmingham  90
Leicester   Lincoln 82
Sheffield   Birmingham  122
Lincoln Doncaster   63
Sheffield   Doncaster   29
Bristol Reading 130
Hull    Nottingham  145
Blackpool   Leeds   116
Birmingham  Bristol 139
Manchester  Leeds   64
Carlisle    Blackpool   140
Leicester   Northampton -61
Newcastle   York    135
Glasgow Moffat  -28
Leicester   Sheffield   100
Carlisle    Liverpool   -30
Birmingham  Manchester  129
Oxford  Bristol 116
Leeds   Hull    89
Edinburgh   Carlisle    154
Nottingham  Sheffield   61
Liverpool   Manchester  56
Carlisle    Glasgow 50
Sheffield   Lincoln 74
York    Doncaster   55
Newcastle   Edinburgh   177
Leeds   Sheffield   53
Northampton Oxford  68
Manchester  Carlisle    20

There are 21 cities (nodes) in total and each line in the file shows how much energy is needed to move from one city to another (edges). I want to save the data in to a matrix which can be used for further calculations later on.


Solution

  • If you want to store value in array, create the array then copy the value into it.

      int num[nLines];
      char start_vertex[nLines][250];
      char destination_vertex[nLines][250];
    
    

    Copy the data, you can use the strcpy() for copying string to string in c

    for(int i = 0; i < nLines; i++ ) {
       if (fscanf ...
       ...
       num[i] = valuesPtr[i].num;
       strcpy(start_vertex[i], valuesPtr[i].start_vertex);
       strcpy(destination_vertex[i], valuesPtr[i].destination_vertex);
       ...
    }
    
    

    The test:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct value {
      int num;
      char start_vertex[250];
      char destination_vertex[250];
    } value;
    
    int main()
    {
      const int nLines = 43; // number of lines in my text file
      FILE * fptr;
      value * valuesPtr = malloc(sizeof(value) * nLines);
    
      if (!valuesPtr) {
        puts("cannot allocate memory");
        return -1;
      }
    
      if((fptr = fopen("text.txt", "r")) == NULL)
      {
        perror("Error opening file");
        return -1;
      }
    
      // The array for storing all num that you get from energy file
      int num[nLines];
      // The array of string for storing all start_vertex that you get from energy file
      char start_vertex[nLines][250];
      // The array of string for storing all destination_vertex that you get from energy file
      char destination_vertex[nLines][250];
    
      for(int i = 0; i < nLines; i++ )
      {
          if (fscanf(fptr, "%249s %249s %d",
                     valuesPtr[i].start_vertex,
                     valuesPtr[i].destination_vertex,
                     &valuesPtr[i].num) != 3) {
            printf("errored file line %d\n", i);
            break;
          }
    
          num[i] = valuesPtr[i].num;
          strcpy(start_vertex[i], valuesPtr[i].start_vertex);
          strcpy(destination_vertex[i], valuesPtr[i].destination_vertex);
    
          printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
                 valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
    
          printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
                 start_vertex[i], destination_vertex[i], num[i]);
      }
      free(valuesPtr);
      fclose(fptr);
    
      return 0;
    }