me*****@gmail.com wrote:
Ok. That is what the error message was telling me and I just wanted to
make sure. Now, I have another question:
let's say that I have an implementation as follows
#include <iostream>
using namespace std;
int x [ ][ ] ; // global variable matrix
You must specify all but the first dim of 'x', e.g.
int x[][2];
int len_x; // will be the length of the array in x direction
// modify_ar modifies the array
void modify_ar (int a, int b, ar[][]){
You must specify all but the first dim of 'ar', e.g.
void modify_ar(int a, int b, ar[][2]) {
ar[a][b] = 5;
}
int main()
{
scanf("%d", len_x); //read the length now
x= new float [len_x][2]; //initialize the matrix now
Hmm, you declare 'x[][]' as 'int', now you want it to
be 'float' - it can be one or the other, but not both.
}
So as I tried to show above, I need to read an integer from a file and
create a matrix using that integer as a length. In addition, there is
this function modify_ar I will use to modify the array I just created.
Thanks ,
Mehmet Canayaz
Use 'vector' instead:
//-------------------------
#include <vector>
#include <iostream>
// class Matrix derived from a vector of vectors of T.
// all of the features of 'vector' are available to Matrix.
template< class T >
class Matrix : public std::vector< std::vector< T > >
{
public:
Matrix() : std::vector< std::vector< T > >()
{
}
Matrix(int row, int col) :
std::vector< std::vector< T > >(row, std::vector< T >(col))
{
}
Matrix(const Matrix& m) : std::vector< std::vector< T > >(m)
{
}
// dump my contents to 'os' for debugging
std::ostream& dump(std::ostream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;
for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}
os << std::endl;
return os;
}
};
int main()
{
int rows;
// read 'rows' from stdin
std::cerr << "Enter number of matrix rows: ";
std::cin >> rows;
// we could also read the number of columns
// from stdin, but in this example we've
// hard-coded the column counts.
// make 'x' as a 2 dim matrix of ints with
// 'rows' rows and 2 cols
Matrix<int> x(rows, 2);
// dump the initial contents of 'x' - should be
// all zeroes
x.dump(std::cout, "'x' initial contents");
int v = 0;
// put some values into 'x'
for (int row = 0; row < x.size(); row++)
for (int col = 0; col < x[row].size(); col++)
x[row][col] = v++;
// dump the new contents of 'x'
x.dump(std::cout, "'x' with values added");
// make 'b' as a copy of 'x'
Matrix<int> b(x);
// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'x'");
// erase 'x'
x.clear();
// demonstrate that the copy-constructor
// actually copied the data from 'x' to 'b'
x.dump(std::cout, "'x' after x.clear()");
b.dump(std::cout, "'b' after x.clear()");
// copy 'b' to 'x' using the '=' operator
x = b;
x.dump(std::cout, "'x' after 'x = b'");
// make 'c' as a 2 dim matrix of floats with
// 'rows' rows and 4 cols
Matrix<double> c(rows, 4);
c.dump(std::cout, "'c' of doubles");
return 0;
}
Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.