array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rudra

    array

    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("fi le", 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?
  • Sam

    #2
    Re: array

    rudra writes:
    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("fi le", 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?
    Your error is in "infile >Matrix[ii][jj]". This reads the next number from
    your input file. This is done without regards to newlines. In this context,
    a newline is no different than any other whitespace character.

    What you should do is use std::getline() to retrieve the next line of input
    from the file into a std::string. Use that to instantiate a
    std::istringstr eam, then parse out the individual numbers from
    std::istringstr eam just like you're doing from the file stream object. Any
    remaining, unused numbers that you do not parse from the line will simply
    get discarded, and on the next iteration of the loop std::getline()
    retrieves the next line from the file, as your original intentions were.


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkh 5IPMACgkQx9p3GY HlUOLodQCfc4/Q8ERdAluqbabnj6 b+G6NM
    GfoAn36gC+Ofaq5 risqPorKweHIquP lQ
    =Ka5L
    -----END PGP SIGNATURE-----

    Comment

    • Eric Pruneau

      #3
      Re: array


      "rudra" <bnrj.rudra@gma il.coma écrit dans le message de news:
      9ed4fb9b-ca54-403f-85b8-e51129854874...le groups.com...
      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("fi le", 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.tx t");
      typedef vector<intVecIn t;
      VecInt rowvec(column);
      vector<VecIntMa trix(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



      Comment

      • James Kanze

        #4
        Re: array

        On Jul 13, 3:09 am, "Eric Pruneau" <eric.prun...@c gocable.cawrote :
        "rudra" <bnrj.ru...@gma il.coma écrit dans le message de news:
        9ed4fb9b-ca54-403f-85b8-e51129854...@26 g2000hsk.google groups.com...
        [...]
        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:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...