for (int w=0;w<10;w++)
{
//some lines of code
unsigned long num = x.to_ulong();
cout <<"Decimal form is: " << num<< end;
// if we've hit 15 items in the current row (i.e. 15 columns), then shift to the next row
if (row->size() == 15)
++row;
// add this to the next column in the current row
row->push_back(num);
}
// resize the last row
row->resize(15); // this will ensure that there are 15 columns in the last row
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
#include <vector>
#include <algorithm>
class matrix
{
public:
matrix()
: n_columns(0),
n_rows(0)
{
}
matrix(int columns, int rows)
: n_columns(columns),
n_rows(rows),
data(columns * rows)
{
}
int operator ()(int row, int column)
{
return data[column + (row * n_columns)];
}
std::vector<int>::iterator begin()
{
return data.begin();
}
std::vector<int>::iterator end()
{
return data.end();
}
private:
std::vector<int> data;
int n_columns;
int n_rows;
};
int main()
{
matrix m(10, 20);
// Fill the matrix here.
// Find the maximum value in the matrix.
int maximum = *std::max_element(m.begin(), m.end());
// Get element at 4,6
int value = m(4, 6);
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
class matrix
{
matrix(int columns, int rows)
: n_columns(columns),
n_rows(rows),
data(columns * rows)
{
}
...
private:
std::vector<int> data;
int n_columns;
int n_rows;
};
You might want to be careful with your order of initialization.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
You might want to be careful with your order of initialization.
The constructor initialises all of the members with the constructor parameters, so everything should be fine. Now if I'd used 'n_rows' & 'n_columns' to initialise 'data', that would be different.
I avoid dependencies of member declaration; it's too open to accidental error.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Bookmarks