0

Im making a program to manage a sparse matrix, i can add elements, print the columns or lines, etc.

In the beggining, i can use a text file with a matrix and go from there or start with a "empty matrix". Basically if the first thing i do is add a 4 to the position [3] [4] now i have a matrix with 3 rows and 4 columns with the all of the elements beings 0 except for the 4.

So basically, the matrix can change size as i add more values, if i add something to the position [5] [5] now the matrix has 5 rows and 5 columns.

I dont know how i do this, i know i cant initialize an array with no elements so i want to know how i do it so the array can change size.

MuchoG
  • 63
  • 5
  • 1
    Looks like you already have a few answers [here](https://stackoverflow.com/questions/49909416/making-a-2d-array-from-a-text-file) – MFisherKDX Apr 18 '18 at 23:16
  • @MFisherKDX isn´t it different? On that case i read the matrix from a text file, on this i dont and i keep adding values to a "empty" matrix that keeps changing size – MuchoG Apr 18 '18 at 23:30
  • subtlety different, yes. but if you understand @Pablo's [accepted answer](https://stackoverflow.com/a/49910132/8767209) then you will understand how to do what you are asking. As it stands, this question will probably get downvoted due to lack of effort (ie: no code shown). – MFisherKDX Apr 18 '18 at 23:36
  • Arrays can't change size in C. You need to use dynamic allocation with `malloc()` and `realloc()`. – Barmar Apr 19 '18 at 00:22
  • If you only want to store the sparse matrix without the overhead of a full matrix, then look again at my solution in your other question. The `struct matrix_info *info` is what you need. Adding a new dimension is easy, reallocate space for one more element and store the col, row and value in the new `info` list. You are not going to solve this with pure arrays, as you cannot change the size of an array. Once you declare an array, the size remains constant. – Pablo Apr 19 '18 at 20:33

0 Answers0