-1

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.

1 Answers1

0

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;
}
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • How do I go about doing it? Could you show me an example using my code please – Usama Navid Apr 12 '20 at 20:31
  • Perfect, thanks. How can I access the elements in that array, could you show me a test version of that too please. For example a code which will access the data from this array and print it in to the terminal – Usama Navid Apr 12 '20 at 20:39
  • look at the printf function. – Hitokiri Apr 12 '20 at 20:43
  • 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]); – Usama Navid Apr 12 '20 at 20:50
  • num[i] = valuesPtr[i].num, that copy the value of num of node i to array num – Hitokiri Apr 12 '20 at 20:56
  • strcpy is a function that allow you to copy tring to string – Hitokiri Apr 12 '20 at 20:56
  • printf, just print out the results of all array for testing – Hitokiri Apr 12 '20 at 20:57
  • Is it possible if you can show me by using a table so I understand where the information is being stored – Usama Navid Apr 12 '20 at 21:08
  • What kind of data structure did you use here? Sorry I am new to this – Usama Navid Apr 12 '20 at 21:10
  • `I want to save the data in to a matrix` in your question. So i store data in three arrays. Each array store each value of struct that you declared above – Hitokiri Apr 12 '20 at 21:11
  • for example, array `num` stores all number that you get from the file – Hitokiri Apr 12 '20 at 21:12
  • int num[nLines]; char start_vertex[nLines][250]; char destination_vertex[nLines][250]; – Usama Navid Apr 12 '20 at 21:13
  • yup, these are three arrays that i used to store all values you get from the file txt – Hitokiri Apr 12 '20 at 21:15
  • Is it possible if you can comment on your could comment on the code so I could understand it please – Usama Navid Apr 12 '20 at 21:15
  • It would be really appreciated if you could sow me ow this matrix looks like in a table format – Usama Navid Apr 12 '20 at 21:19
  • it's all thing that i can show you. You have to learn the knowledge of array – Hitokiri Apr 12 '20 at 21:21
  • it's just a simple way to store the value in to array. you have to think about what you want, what you do after,etc – Hitokiri Apr 12 '20 at 21:22
  • I want to write a code for the program that will calculate the least amount of energy needed to go from one city to another and I also want it to print all the cities that it will go through in order to get the least energy spent path. I am not sure how I will use the arrays to do that. Have you got some advise that you can give me please :) – Usama Navid Apr 12 '20 at 21:28
  • You have to learn about array first. You know nothing before trying. So, try with your idea. Make the code from the scratch, you will learn form the errors and problems. Any way, write the code with your idea, then ask the other if you can not figure it out. Good luck – Hitokiri Apr 12 '20 at 21:32