array

Discussion in 'C++' started by rudra, Jul 12, 2008.

  1. rudra

    rudra Guest

    dear all, being a starter with C++, i am getting a problem with array.
    i have a (say),NxN array in a file, from which i have to read the nXn
    (n<N, obviously) chunk, and with each iteration, the size will
    increase. the first problem of dynamic allocation is solved. but i am
    getting a trouble that the code is reading first nxn element along row
    and not the chunk. suppose i have a array:
    1 2 3 4 5 6
    2 3 4 5 6 7
    3 4 5 6 7 8
    4 5 6 7 8 9
    5 6 7 8 9 0
    and if i write the code
    int main() {

    ifstream infile ;
    infile.open("file", ios::in);

    const unsigned row = 4 ; // number of rows of the matrix, does not
    match the row of file
    const unsigned column = 3 ; // number of columns of the matrix;does
    not match the column of file
    // float matrix[row][column] ;

    int Matrix[row][column-1] ;

    for(int ii = 0; ii < row; ++ii) {
    for(int jj = 0; jj < column; ++jj) {
    infile >> Matrix[ii][jj] ;
    cout << Matrix[ii][jj] << " " ;
    }
    cout << "\n" ;
    }

    return 0 ;

    }
    then we get the output:
    1 2 3
    4 5 6
    2 3 4
    5 6 7
    where i am intending it should give
    1 2 3
    2 3 4
    3 4 5
    4 5 6

    will you people plz help me?
    rudra, Jul 12, 2008
    #1
    1. Advertising

  2. rudra

    Eric Pruneau Guest

    "rudra" <> a écrit dans le message de news:
    ...
    > dear all, being a starter with C++, i am getting a problem with array.
    > i have a (say),NxN array in a file, from which i have to read the nXn
    > (n<N, obviously) chunk, and with each iteration, the size will
    > increase. the first problem of dynamic allocation is solved. but i am
    > getting a trouble that the code is reading first nxn element along row
    > and not the chunk. suppose i have a array:


    Your program are not doing any dynamic allocation.

    int StackArray[5]; // an array of 5 elements stored on the stack
    int* HeapArray = new int[5]; // an array of 5 elements stored on the
    heap

    Here, HeapArray is dynamically allocated, but StackArray is not.


    > 1 2 3 4 5 6
    > 2 3 4 5 6 7
    > 3 4 5 6 7 8
    > 4 5 6 7 8 9
    > 5 6 7 8 9 0
    > and if i write the code
    > int main() {
    >
    > ifstream infile ;
    > infile.open("file", ios::in);
    >
    > const unsigned row = 4 ; // number of rows of the matrix, does not
    > match the row of file
    > const unsigned column = 3 ; // number of columns of the matrix;does
    > not match the column of file
    > // float matrix[row][column] ;
    >
    > int Matrix[row][column-1] ;
    >
    > for(int ii = 0; ii < row; ++ii) {
    > for(int jj = 0; jj < column; ++jj) {
    > infile >> Matrix[ii][jj] ;
    > cout << Matrix[ii][jj] << " " ;
    > }
    > cout << "\n" ;
    > }
    >
    > return 0 ;
    >
    > }
    > then we get the output:
    > 1 2 3
    > 4 5 6
    > 2 3 4
    > 5 6 7
    > where i am intending it should give
    > 1 2 3
    > 2 3 4
    > 3 4 5
    > 4 5 6
    >
    > will you people plz help me?



    Here is a very simple solution (you should add at least some error checking)
    it wont work if one of the line (in the file) has more than 256 characters.

    #include <vector>
    #include <sstream>

    using namespace std;

    int main()
    {
    const unsigned row = 4 ;
    const unsigned column = 3;
    ifstream infile("file.txt");
    typedef vector<int> VecInt;
    VecInt rowvec(column);
    vector<VecInt> Matrix(row,rowvec);

    // I assume each line in your file contain less
    // than 256 characters, if not, the easiest thing
    // to do is to set MaxCol to the number of char
    // of your longest line
    const unsigned MaxCol = 256;
    char buffer[MaxCol];

    for(unsigned ir = 0; ir < row; ++ir)
    {
    // getline will read until it reach MaxCol characters
    // or until it reach a newline character
    infile.getline(buffer, MaxCol);
    // now extract data using istringstream
    istringstream iss(buffer);
    for(unsigned ic = 0; ic < column; ++ic)
    iss >> Matrix[ir][ic];
    }
    return 0;
    }



    ------------------------

    Eric Pruneau
    Eric Pruneau, Jul 13, 2008
    #2
    1. Advertising

  3. rudra

    James Kanze Guest

    On Jul 13, 3:09 am, "Eric Pruneau" <> wrote:
    > "rudra" <> a écrit dans le message de news:
    > ...


    [...]
    > Here is a very simple solution (you should add at least some
    > error checking) it wont work if one of the line (in the file)
    > has more than 256 characters.


    Which is easily fixed by using std::string instead of char[256].

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,052
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    21,994
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    489
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    209
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    473
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page