954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading in 2 arrays from one file

Hey guys. I have a problem. I have to read in 2 arrays from 1 file. I know how to read in an array with known elements but this time the number of rows/columns are in the file. The first line has the dimensions of the array(rows columns) then the array is below it. The next line is the dimensions of the second matrix(rows columns). Normally I would do:

while(file>>a>>b>>c>>d)


But the number of elements is not know. I dont get how to just read the first line then use those numbers to define how many lines to read and then do the same thing over again on the second matrix. Thanks in advance.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

read the first two integers before that loop. something like this:

int nRows, nCols;
int* array;
file >> nRows >> nCols;
array = new int[nRows * nCols * sizeof(int))];
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks for the quick response. this might be a dumb follow up, but what does the size(int) do at the end of that new array?

and one more thing... i dont think i was clear when i said this in the original post. It has to be a multi dimensional array so would i initialize it like this?

array = new int[nRows][nCols];
Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

Thanks for the quick response. this might be a dumb follow up, but what does the size(int) do at the end of that new array?

and one more thing... i dont think i was clear when i said this in the original post. It has to be a multi dimensional array so would i initialize it like this?

array = new int[nRows][nCols];


No you cant do this. Also Mr. Dragons code

int nRows, nCols;
int* array;
file >> nRows >> nCols;
array = new int[nRows * nCols * sizeof(int))];


assumes that the array locations of a two dimensional array are contiguous which may not necessarily be true if the optimizing compiler decides to align to the double word boundaries ( microprocessor related ).

Try somthing like:

int main()
{
    int** a = new int*[3] ;

    for( int i = 0; i < 3; ++i )
    {
        a[i] = new int[3] ;
    }

    for( int i = 0; i < 3; ++i )
    {
        for( int j = 0; j < 3; ++j )
        {
            a[i][j] = i + j + i * j ;
        }
    }

    for( int i = 0; i < 3; ++i )
    {
        for( int j = 0; j < 3; ++j )
        {
            cout << a[i][j] ;
        }
        putchar( '\n' ) ;
    }

    return 0;

}


Mr. Dragon, I dont think there is need in C++ to supply the size of the data type whose array you want to create since new automatically handles it. You just need to specify the dimensions or the slots required.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>>No you cant do this.
you can if you treat the 1d array as a 2d array. If you want column 0 of row 1, where row = desired row number (1), columns = number of columns per row, and col = desired column number (0)

array[ row * columns + col] = 1;


Actually, in C++ I would use a vector of vectors instead of a C-style array, but I doubt that was the intent of the assignment.

vector<vector<int>> array;
Mr. Dragon, I dont think there is need in C++ to supply the size of the data type whose array you want to create since new automatically handles it. You just need to specify the dimensions or the slots required.

your are right -- I was thinking C not C++.:lol:

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks for the help gents. I cant treat the array as a 1d array because I need to perform matrix opperations on it later in the program but I think i worked a few of the bugs out. however ive been workin on this and came across a few other problems as well. I need to do this with a class and constructor and this is what I have.

#include <iostream>
#include <fstream>

using namespace std;

class Matrix {
    public:
        Matrix(int,int);
    private:
        int rows, cols;
        int EL[rows][cols];
};

Matrix::Matrix(int r,int c)
{
    rows = r;
    cols = c;
}

int main()
{
    int a=0,b=0;
    
    ifstream infile("input.dat");
    
    infile>>a>>b;
    Matrix A(a,b);    
    return 0;
}



I think the general structure is correct but i know i have santax errors. Any suggestions?

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

You have made quite a few mistakes there. It would be good if you went back to study the material provided to you before you dive head first into coding.

But still I am pointing out the mistakes to you.

#include <fstream> // you forgot to include this i think
using namespace std ;

class Matrix {
    private:
         Matrix(int,int); 

// constructor cant be private since it gets 
// called whenever a new object is created in main
// private data members and functions can be accessed only
// in the class in which they are present

         int rows, cols;
        int EL[rows][cols]; 

// you cant have variable length arrays in C / C+
// why bother with this, either make it a double pointer to int
// or better yet use vectors of C++.
 };

Matrix::Matrix(int r,int c)
{
    rows = r;
    cols = c;
}

int main()
{
    int a=0,b=0;

    ifstream inflie("input.dat"); // spelling mistake

     infile>>a>>b ; // missing semi colon

     Matrix A(a,b);    
    return 0;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

lol wow sorry. i threw that code together and i didnt even see the basic errors. but i believe this should be more like it:

#include <iostream>
#include <fstream>

using namespace std;

class Matrix {
    public:
        Matrix(int,int);
    private:
        int rows, cols;
        int **EL;
};//Class Matrix

Matrix::Matrix(int r,int c)
{
    rows = r;
    cols = c;
    **EL = EL[rows][cols];
}//Class Matrix constructor

int main()
{
    //Initalize Variables
    int r1=0,c1=0;
    
    //Load data
    ifstream infile("input.dat");
    
    //Get matrix A dimensions
    infile>>r1>>c1;
    
    //Create object A with paramaters r1 & c1 (rows & cols)
    Matrix A(r1,c1);
    
    return 0;
}



that should work correct? it comples and runs.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 
#include <iostream>
#include <fstream>

using namespace std;

class Matrix {
    public:
        Matrix(int,int);
    private:
        int rows, cols;
        int **EL;
};//Class Matrix

Matrix::Matrix(int r,int c)
{
    rows = r;
    cols = c;
     **EL = EL[rows][cols]; // you cant do this in C / C++
   
// when you create pointer variables you cant use them until you have
// allocated them memory. A pointer points to just junk unless done so.
// IN this part you need to paste the code for two dimensional matrices
// which I just showed back to you a few posts back.

// Again I ask you the same question, "why not use vectors" ?
// It makes the task a lot easier by taking away the gory mem
// management issues away from you, and also provides you with
// bound checking.

// For information on vectors visit  <a href="http://www.cppreference.com/cppvector/all.html">here</a> 

// Also if you want a reference while building your matrix classes 
// you can take the help of  <a href="http://math.nist.gov/mv++/">this source code</a> 

 }


Also if you want two dimensional matrices in C++ vectors look here

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

thanks. for the info on vectors. we have not covered them in our class and the program was supposed to be written using a class. thanks for all the help. i really appreciate it.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

If you have got the working program, please post it here so that the others who refer this thread can see the solution and benefit from it.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You