LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2008, 04:03 PM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Rep: Reputation: 23
array ,C++


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:
Quote:
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
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:
Quote:
1 2 3
4 5 6
2 3 4
5 6 7
where i am intending it should give
Quote:
1 2 3
2 3 4
3 4 5
4 5 6
will you people plz help me?
 
Old 07-12-2008, 04:08 PM   #2
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
The istream operator>> reads always an int from your file. Since you don't check for new lines in your input you get the first output, not the supposed second. Try to store the size of your matrix in the file. something like:

Code:
6
5
1 2 3 4 5 6
 ...
 
Old 07-12-2008, 04:14 PM   #3
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
yes...and i have to put an endl...right? question is how!!!
 
Old 07-12-2008, 04:26 PM   #4
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
You can't do that with the operator>>. You can specify your file format:

cols
rows
.. here goes your matrix...

Or you can try reading the file line by line. Have a look for:
istream.getline(...)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
array c++ harrylee2003 Programming 3 10-14-2005 05:04 PM
Array help DropHit Linux - Software 0 06-30-2005 09:31 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
2-D Array clowns119 Programming 10 09-24-2003 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration