array

R

rudra

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?
 
E

Eric Pruneau

rudra said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top