global variable matrix

M

memocan

#include <iostream>
using namespace std;

int x[][]; //global variable matrix

int main()
{
x= new float [1][2]; //initialize the size now

}

I am getting the error
" declaration of `x' as multidimensional array must have bounds for
all dimensions except the first"

Can someone explain why??

Thanks;
Mehmet Canayaz
 
V

Victor Bazarov

#include <iostream>
using namespace std;

int x[][]; //global variable matrix

int main()
{
x= new float [1][2]; //initialize the size now

}

I am getting the error
" declaration of `x' as multidimensional array must have bounds for
all dimensions except the first"

Can someone explain why??

You declared 'x' to be a multi- (two-) dimensional array. The language
rules require such a declaration to contain the sizes of all dimensions
except the first (the closest to the variable). You didn't provide
any sizes. The compiler cannot accept that, and therefore complains.

Please read the FAQ about dynamic memory management and multidimentional
arrays. And in general, prefer using 'vector' over your own allocation
of arrays.

V
 
M

memocan

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
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[][]){
ar[a] = 5;
}

int main()
{

scanf("%d", len_x); //read the length now
x= new float [len_x][2]; //initialize the matrix now

}


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
 
C

CrayzeeWulf

Victor said:
Please read the FAQ about dynamic memory management and multidimentional
arrays. And in general, prefer using 'vector' over your own allocation
of arrays.

Ok. That is what the error message was telling me and I just wanted to
make sure. Now, I have another question:
It might be helpful to follow Victor's advice first.
 
L

Larry I Smith

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] = 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::eek:stream& dump(std::eek:stream& 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
 
L

Larry I Smith

Embedded code additions show how to make a global
matrix of ints that is resized at runtime.

Larry I Smith wrote:
[snip]
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::eek:stream& dump(std::eek:stream& 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;
}
};

// 'a' is a 2 dim matrix of ints with zero rows and cols
Matrix said:
int main()
{
int rows;

// read 'rows' from stdin
std::cerr << "Enter number of matrix rows: ";
std::cin >> rows;

// read 'cols' from stdin
std::cerr << "Enter number of matrix columns: ";
std::cin >> cols;

// resize the global 'a' to have 'rows' rows and
// 'cols' columns
// 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 doubles with
// 'rows' rows and 4 cols
Matrix<double> c(rows, 4);
c.dump(std::cout, "'c' of doubles");

return 0;
}

Regards,
Larry
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top