I’m trying to create a 2D array from the data read from .txt file.
The data in ‘data.txt’ look like belows:
A;B;4
A;C;5
B;C;8
Assume it is symmetry.
Is there any way to create a 2D matrix [i][j] and print in d[i][j] = value?
Appreciate your comments/suggestion.
2D arrays are useful when the dimensions are known at compile time. If this is the case, other answers can be useful. If not, a 2D array isn’t for you.
I suggest using a simple 1D array, allocated (
malloc) to have n*n entries.Then, to access cell i/j, use
array[i*n+j].Another approach is to allocate an array of pointers to arrays. Creating it is more complicated, but you can access it as
array[i][j].