-2

I need to scan some integers into a 2D array (square matrix). After running the file, the matrix will be entered in this form (line by line):

EDIT: My biggest problem is determining the size of the array without losing any of these input lines.

4 4 1 1
4 4 1 1
3 2 2 3
4 1 1 1

The code should work for any nxn matrix. I then have to be able to do some arithmetic with the matrices.

I already wasted many hours trying to figure this out and it's really depressing me that I made 0 progress. I thought that, to figure out the matrix size, I need to scan the first line, but when I scan the first line I lose those integers!? I think I'm also supposed to use pointers.

I have almost no experience with C, maybe someone with more experience can show me what the code should look like so I can learn.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    Show us what you have done so far. – yoonghm Sep 29 '18 at 02:54
  • Possible duplicate of [Malloc a 2D array in C](https://stackoverflow.com/questions/36890624/malloc-a-2d-array-in-c) – Marek Sep 29 '18 at 02:55
  • I wish I had anything to show, but I'm stuck on trying to find the size of the array while at the same time putting the input lines into the array. –  Sep 29 '18 at 03:06
  • Step 1) Read with `fgets()` into a wide buffer, say 1000 `char`. 2) parse for `int`s. 3) Now code knows the number of `int` in a line. 4) rewind the file 5) allocate 6) read the matrix – chux - Reinstate Monica Sep 29 '18 at 03:17
  • Ayrton Twigg, **Important**:: are you reading from a _file_ or `stdin` or possible either? – chux - Reinstate Monica Sep 29 '18 at 03:23
  • "show me what the code" without posting your code attracts down-votes. Post your coding effort, even if poor is far far better. – chux - Reinstate Monica Sep 29 '18 at 03:27
  • Ok, I'm gonna try that! Thanks! I have found how to parse from char to int! –  Sep 29 '18 at 03:44
  • Without any code, it's difficult to understand what you want to achieve, let alone where you are stuck. You must have written some code, right? Even if it doesn't work. Just posting a problem statement, when the question is specifically about code, make people believe that you are just posting your homework, to be done by others. Use this question (https://stackoverflow.com/questions/36044429/how-do-i-scan-into-a-2d-char-array-without-scanning-in-the-enter-space-chars/36044704), and it's answers as a starting point, try yourself, and then try updating the question with your specific query. – Tushar Sep 29 '18 at 03:50

3 Answers3

3

There are four approaches you can take:

  1. encode the size of the matrix in the first line of the file, most coding exercises do this to simplify data loading. It also had precedents in many protocols like HTTP that encodes size explicitly in Length header. This is simplest to implement if you have control of the data format

  2. Use a data structure that you can dynamically expand, like linked list or dynamically grown array (similar to C++ vector). You'll have to implement or find a library that implements these data structures first, but then you'll be able to deal with variable sized data easily.

  3. Do two passes on the data, first pass is to figure out the size of the array you need to allocate, then fseek() back the file to beginning, and read the data in the second pass.

  4. A variant of the previous approach is to read the data into a variable size data structure, then convert them to regular array (or some other data structure that's more appropriate to work with)

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
0

I recommend checking how malloc works, it allows you to dynamically allocate memory. Good place to start is example of wikibooks.

It appears you are filling out the matrix while reading file and you don't know how much data there is. I think what you could do is use linked list of pointers to other linked lists, which allows you to make a variable size 2D matrix.

Some example to start.

Good luck!

Marek
  • 1,413
  • 2
  • 20
  • 36
  • I looked into it before, but my problem is figuring out the size of the array before I can allocate some memory to it... I couldn't find how to do that anywhere. –  Sep 29 '18 at 03:01
0

The null character (\0) is used to mark the end of a string. It is automatically appended at the end of any char* you have. This means that you could read the whole file, then split each line (\n) and then put them together into the 2D char array you are looking for.

ecstrema
  • 543
  • 1
  • 5
  • 20
  • I was considering something like that, but I couldn't find a good way to convert chars to ints. –  Sep 29 '18 at 03:15
  • What about getting the first line of your file, then getting it's length by iterating through it while you don't find that null char? – ecstrema Sep 29 '18 at 03:17