Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array

Reply
Thread Tools

array

 
 
rudra
Guest
Posts: n/a
 
      07-12-2008
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?
 
Reply With Quote
 
 
 
 
Eric Pruneau
Guest
Posts: n/a
 
      07-13-2008

"rudra" <(E-Mail Removed)> a écrit dans le message de news:
9ed4fb9b-ca54-403f-85b8-e51129854874...oglegroups.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("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



 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 3:09 am, "Eric Pruneau" <(E-Mail Removed)> wrote:
> "rudra" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...


[...]
> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments