Hello, I'm just wondering really..

I have a 2D matrix but I've defined it as a 1D matrix. So for example:

double *matrix = new double[N*M];

My question is, I'm going to be manipulating data very shortly and want to read this data in from a text file..

Say if the contents of the text file was:

0 1 0 1 0 0 0 0
0 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1

I know that it is possible, I'm just confused on how I would go about adding the rows and columns (into this style)

Any help would be great, thanks =)

Thanks for your reply.

I would like to know how to read a matrix from a text file into the 1d matrix..

If I had a file called: matrix1.txt, I could then read it into the matrix and output it in main..

If that makes sense =)

Here is some of the code I've been working on atm:

matrix = new double[N*M]; // dynamically assign the size of the matrix
    // This is the test matrix
    double array[15] = { 0, 1, 1, 
                          1, 0, 0,
                          1, 1, 1,
                          1, 1, 1, 
                          1, 1, 0
                        };
    
    for(int i=0; (i < M); i++) {
        
        for (int j=0; (j < N); j++)
        {
            matrix[i*N+j] = (double) array[i+j];
            cout << matrix[i*N+j] << "\t";
        
        }
        cout << "\n";
    }

When I try to output it though, it does this:

0 1 1
1 1 1
1 1 0
1 0 0
0 0 1

Which isn't right.. Any advice?

My quess is it depends on what N and M are...

matrix[i*N+j] = (double) array[i+j];
                ----^---

Why? Aren't they both doubles?

Hey thanks for the reply (You were right, they are doubles.. Not needed)

when I print out i+j it gives me:
0
1
2
1
2
3
2
3
4
3
4
5
4
5
6

Which looks weird.

Hey

Even if I did this..

for(int i=0; (i < N); i++) {
        
        for (int j=0; (j < M); j++)
        {
            matrix[i*N+j] = array[i+j];
           
            cout << matrix[i*N+j] << "\t";
    
        }
        cout << "\n";
    }

What should the numbers be print out for i+j? (Sorry to ask, just really confusing)

i+j in fixed:
0
1
2
3
4
1
2
3
4
5
2
3
4
5
6

Edited 4 Years Ago by phorce: n/a

Think about it! What did you do for matrix?

Write the loop out on a piece of paper and follow it with a pencil, changing i,j,and the index and you'll see what's wrong.

This is called desk checking and is the single greatest debugging aid you can use. Printing the values like you did is the second.

GOT IT! :) Thank you haaa!

Now to read in from a text file.. Wish me luck!

I am assuming that you need to keep track of the data as if it is still in a two dimensional array after you read it in. Basically, you have to keep track of three things. You need an index for the one dimensional array, and you need a “row” index and a “column” index for the two dimensional array. Obviously, the size of the 1D array must be at least the number of rows times the number of columns. The relationship between the indices is:

1D_Index = Row_Index * LengthOf Row + Column_Index

Of course, you can reverse the rows and columns, if that works better for you.

Do you know the number of rows and columns before you start? This is really kind of important if you are going from one array to the other.

If my initial assumption was incorrect, and once you read in the data, you don’t need to keep track of what row it was in, things are simpler. You just need an index for the one dimensional array. Read each piece of data in, starting at the first line (row), and just store it into the 1D array using the index. Increment the index after each reading each piece of data, regardless of whether you change lines. Most input methods are actually set up to read in data this way, and will go left-to-right, line by line.

By the way, when the compiler allocates memory for a multi-dimensional array, it still allocates a block of sequential memory addresses. So, the memory actually ends up allocated like a one dimensional array.

Hey, thank you very much for your reply :)

To answer your question:
Do you know the number of rows and columns before you start? This is really kind of important if you are going from one array to the other.

- Yes I'll know the size of the matrix. (e.g. 10 x 10)

I think I have something of an idea on how to read the matrix in:

I'm going to have a function that reads the contents of the text file into an array. I'm unsure whether or not I will just define the file inside main and then process the file and store it into an array..

OR actually read the contents of the file inside main and then pass the array through a method..

Thoughts? =)

Text file =).. I would like to be able to change the values in the text file, run the executable and it'll create the matrix.. But, I'm not that far yet!

This article has been dead for over six months. Start a new discussion instead.