Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   array (http://www.velocityreviews.com/forums/t625259-array.html)

rudra 07-12-2008 09:00 PM

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("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?

Eric Pruneau 07-13-2008 01:09 AM

Re: array
 

"rudra" <bnrj.rudra@gmail.com> 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




James Kanze 07-13-2008 08:33 AM

Re: array
 
On Jul 13, 3:09 am, "Eric Pruneau" <eric.prun...@cgocable.ca> wrote:
> "rudra" <bnrj.ru...@gmail.com> a écrit dans le message de news:
> 9ed4fb9b-ca54-403f-85b8-e51129854...@26g2000hsk.googlegroups.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.kanze@gmail.com
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


All times are GMT. The time now is 05:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57