I am trying to get an understanding of how to work with matrices in C++. The code at the bottom is supposed to take an input matrix and return the places where there are 0s. However, I am getting the following errors:
matrix.cpp:47:3: error: no matching function for call to 'make_zero' make_zero(i,j,l);
^~~~~~~~~
matrix.cpp:8:6: note: candidate function not viable: no known conversion from 'double [i][j]' to
'double (*)[col]' for 3rd argument
void make_zero(int row, int col, double matrix[row][col])
^
1 error generated.
when I try to run the following code:
// Matrix
#include <iostream>
#include <stdio.h>
using namespace std;
void make_zero(int row, int col, double matrix[row][col])
{
int k,l;
for(k=0;k<row;k++)
for(l=0;l<col;l++)
{
if(matrix[k][l]==0)
printf("%d %d\n",k,l);
}
}
int main ()
{
int i = 0,j = 0;
cout << "Enter no of rows of the matrix";
cin >> i;
cout << "Enter no of columns of the matrix";
cin >> j;
double l[i][j];
int p = 0, q = 0;
while (p < i) {
while (q < j) {
cout << "Enter the" << p + 1 << "*" << q + 1 << "entry";
cin >> l[p][q];
q = q + 1;
}
p = p + 1;
q = 0;
}
cout << l << "\n";
make_zero(i,j,l);
}
Any help would be appreciated. Thanks.
There are a bunch of ways to do this with pointers. The most common is
void make_zero(int row, int col, double ** matrix)
defines a pointer (usually rows) to a pointer (usually columns). Unfortunately
double l[i][j];
does not define a pointer to a pointer. If this syntax is supported by the compiler, and the compiler is not required to allow arrays of variable length, it most likely defines a pointer to a 1D array (double l[i*j];) and hides the indexing arithmetic used to convert the array to two dimensions. Anyway, it can't be passed to a double ** because it isn't a double **
Trying to pass as an array is troublesome
void make_zero(int row, int col, double matrix[][NUMBER_OF_COLUMNS])
The number of columns in the array must be known to perform the indexing arithmetic and be provided to any functions called with it. This means that number of columns cannot be changed at run time because the indexing used by the function will be rendered invalid.
Getting around this would require changes to the compiler that will drive it further and further from the C++ standard. A bad idea since there are a number of simple ways around calling functions with multi dimensional arrays. Most depend on arrays of arrays or std::vectors of std::vectors.
And when it comes to these solutions, as far as I'm concerned, the best is don't. I'm not going to cover them.
None of the arrays representing a dimension are guaranteed to be anywhere close to the others in memory, and this limits the CPU's ability to read and cache. Without caching and being able to look ahead, a modern CPU is at a serious performance disadvantage. (Read for more information: Why is it faster to process a sorted array than an unsorted array?)
So what you want is a 1 D array, and those are easy to pass around. The indexing math is also easy, row number * size of column + column number, but you need to pass at least the size of the column around. Rather than scatter the book-keeping around like this:
void make_zero(int row, int col, std::vector<double> matrix)
make a wrapper class like this:
class Matrix
{
private:
std::vector<double> myArray;
size_t nrRows;
size_t nrColumns;
public:
Matrix(size_t rows, size_t columns) :
myArray(rows * columns), // allocate vector to store matrix.
nrRows(rows),
nrColumns(columns)
{
}
size_t getNrRows() const
{
return nrRows;
}
size_t getNrColumns() const
{
return nrColumns;
}
// gets value at row, column and returns a reference so caller can
// modify the value
double& operator()(size_t row, size_t column)
{
// note: No sanity check for row >= nrRows or column > nrColumns
return myArray[row * nrColumns + column];
}
// gets value at row, column and returns a copy so caller cannot
// change the contents of the Matrix
double operator()(size_t row, size_t column) const
{
return myArray[row * nrColumns + column];
}
};
Using the vector gets around a number of common pointer-to-array problems by managing its own memory. No destructor is required and Matrix can be copied and moved without requiring special handling because vector performs all that heavy lifting for us.
And as a usage example, let's make a function that prints the matrix out:
std::ostream & operator<<(std::ostream & out, const Matrix & in)
{
for (size_t i = 0; i < in.getNrRows(); i++)
{
for (size_t j = 0; j < in.getNrColumns(); j++)
{
out << in(i,j) << ' ';
}
out << "\n";
}
return out;
}
And modifying OP's main function to use Matrix we get:
int main()
{
int i = 0, j = 0;
cout << "Enter no of rows of the matrix";
cin >> i;
cout << "Enter no of columns of the matrix";
cin >> j;
Matrix matrix(i,j);
int p = 0, q = 0;
while (p < i)
{
while (q < j)
{
cout << "Enter the" << p + 1 << "*" << q + 1 << "entry";
cin >> matrix(p,q);
q = q + 1;
}
p = p + 1;
q = 0;
}
cout << matrix << "\n";
make_zero(matrix);
}
void make_zero(int row, int col, double ** matrix)
Note, that you need to pass also size of the matrix separately.
Also you can use
std::vector<std::vector<double> >
instead and pass this object by reference, pointer, or just make a copy.
Actually, it works, but your problem in this line also:
double l[i][j];
i, j is unknown during the compile time.
You have 2 ways.
1) dynamically allocate the memory
2) use std::vector<std::vector<double> >. Default constructor already sets zero values. But you can do it manually like this:
#include <iostream>
#include <vector>
void make_zero(std::vector<std::vector<double> > & to_zero) {
for (int i = 0; i < to_zero.size(); ++i) {
for (int j = 0; j < to_zero[i].size(); ++j) {
to_zero[i][j] = 0;
}
}
}
void print_double_vector(const std::vector<std::vector<double> > & to_print) {
for (int i = 0; i < to_print.size(); ++i) {
for (int j = 0; j < to_print[i].size(); ++j) {
std::cout << to_print[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main() {
// your code goes here
int n, m;
std::cin >> n >> m;
std::vector<std::vector<double> > d(n, std::vector<double>(m));
print_double_vector(d);
make_zero(d);
print_double_vector(d);
return 0;
}
http://ideone.com/0X53Yj
Related
I have a map of a room that I have put into a vector of vectors of characters (vector>). The map will look something like this:
# * #
* * D
S * #
where # are walls, * are path areas, S is the start and D is the end. I will not know what the map looks like ahead of time so I want my program to be able to read any map with similar characteristics to the one above.
Thus, I would like to be able to search my vector of vectors to find the coordinates/location of S, so I know where the starting point of the maze is. I have only been able to find examples for just a single vector (one-dimension). Is this possible to do with a vector of vectors (two-dimensions)? If so, how can I do it?
Here is the code I used to create the matrix:
vector<vector<char>> GetMap(int& M, int& N) //function to get the map of a room
{
vector<vector<char>> matrix{}; //give a matrix
char char_buf;
for (int rows = 0; rows < M; rows++)
{
matrix.push_back(vector<char>()); //Put a new empty row in your matrix
for (int cols = 0; cols < N; cols++)
{
cin >> char_buf; //Here we get a char from cin
matrix.back().push_back(char_buf); //That you push back in your sub-vector
}
}
return matrix;
}
First of all, your GetMap function is constantly pushing back new elements. That's a big no no when you already have the size of the matrix available to you (M and N). Also, there is really no need for the size parameters to be of type int&. A simple int is fine and, in most cases, even more efficient.
Rule of thumb: Only use references for non-basic types like vector, string and pretty much all classes.
Also, the fact that you use int& and not const int& doesn't allow you to call the function by passing rvalues (variables without names). For example GetMap(5, 5).
Now, to finally answer your question. Since you already have an idea on how to parse the whole matrix in your GetMap function. I really don't see the problem in creating a similar function that would get the position of a desired character.
The full working code with some enhancements:
#include <iostream>
#include <vector>
using namespace std;
struct Pos{
Pos() : x(0), y(0) {}
Pos(int x, int y) : x(x), y(y) {}
int x;
int y;
};
vector<vector<char>> GetMap(const int height, const int width) //function to get the map of a room
{
//Create the matrix with the constructor (much more efficent than constantly push_back'ing elements)
vector<vector<char>> matrix(height, vector<char>(width));
//Go through every single char in the matrix
for (int rows = 0; rows < height; rows++)
{
for (int cols = 0; cols < width; cols++)
{
cin >> matrix[rows][cols];
}
}
return matrix;
}
Pos getElementPos(const vector<vector<char>>& matrix, const char toFind)
{
int height = matrix.size();
int width = matrix[0].size();
//Go through every single char in the matrix
for (int rows = 0; rows < height; rows++)
{
for (int cols = 0; cols < width; cols++)
{
if(matrix[rows][cols] == toFind){
return Pos(cols, rows);
}
}
}
// In the event that we couldn't find the element
return Pos(-1, -1);
}
int main(){
vector<vector<char>> map = GetMap(5, 5);
Pos dPos = getElementPos(map, 'D');
cout << "\nThe coordinates of D are " << dPos.x << " and " << dPos.y << '\n';
return 0;
}
I have a MatrixTypeheader file which has the following definition :
http://pastebin.com/DMzf1wGB
//Add Two Matrices and store result into another matrix
void Add(MatrixType otherMatrix, MatrixType& resultMatrix);
The implementation of the method above is as such :
void MatrixType::Add(MatrixType otherMatrix, MatrixType& resultMatrix)
{
cout << "Inside Add func!" << endl;
cout << "other matrix : " << endl;
otherMatrix.PrintMatrix();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
resultMatrix.values[i][j] = values[i][j] + otherMatrix.values[i][j];
}
cout << "\n";
resultMatrix.PrintMatrix();
cout << "\n";
}
}
Definition of PrintMatrix() :
void MatrixType::PrintMatrix()
{
//Pre: None
//Post: Matrix is printed row wise
for (int i = 0; i < numRows; i++) {
cout << "[ ";
for (int j = 0; j < numCols; j++) {
cout << values[i][j];
}
cout << "]";
cout << "\n";
}
}
Now in my Main.cpp I have MatrixType array like this : MatrixType matrixStore[10] to store 10 MatrixType objects. The complete code for Main.cpp is here : http://pastebin.com/aj2eEGVS
int rows = matrixStore[index1].getRowSize();
int cols = matrixStore[index1].getColSize();
cout << "The two matrices can be added!" << endl;
cout << "Computing... " << endl;
//Create Result Matrix and a pointer variable to it
MatrixType pResultMatrix = MatrixType(rows, cols);
matrixStore[resultIndex] = pResultMatrix;
//Invoke Add function
matrixStore[index1].Add(matrixStore[index2], matrixStore[resultIndex]);
Now when in my Add() function I do otherMatrix.PrintMatrix() it prints out the values of the matrix invoking the Add() function. And due to this => either I do not have reference to the matrix object invoking the method or the matrix object being passed as the parameter!
Also whenever I do PrintMatrix() after I have added values (in the next round of Switch Case), I always get junk values.
Any solution/explanation to this ?
TIA
The main problem is not in this part of your code but during the matrix creation/initialization. You are storing objects in the array which can be good (especially I like it because it is created on the stack which is much faster, just be aware to do not create too long array to avoid stack overflow) but you have to consider this overall of your code.
For example in the Main.cpp line 38 you copy the content of matrixobject into the array but after that you modify the matrix object which is different from the object which is in the array! This means that the content of the matrix object in the array has some random values. You should modify directly the object in the array, it does not make any sense to have that temporary one.
For example:
matrixStore[index] = MatrixType(rows, cols);
for (int i = 0; i < rows; i++)
{
std::cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++)
{
std::cin >> value;
matrixStore[index].StoreItem(value, i, j);
}
}
I think after this change the part what you copied here should work because there you are working in the array directly.
Some small suggestions:
Pass MatrixType object always as reference if possible. For example, in Add function also the òtherMatrix can be a const ref, your code will be much efficient because the object copy will be not involved. For example:
void MatrixType::Add(const MatrixType& otherMatrix, MatrixType& resultMatrix);
Here the otherMatrix is an input, the resultMatrix is (can be) an output parameter.
In C++ there is real bool type. Avoid bool isAddComp ... if (isAddComp != 0)code, just use if (isAddComp), this is the C++ way.
I would start to use std::vector instead of common arrays, much more flexible and very useful to learn how can you use it.
Personally I would not use ùsing namespace for std it is short and better to read your code (but maybe this is just my code style).
Looking at the code here: http://pastebin.com/aj2eEGVS
It looks like the only reason you're creating a local matrix in all your paths, for example:
MatrixType matrix = MatrixType(rows, cols);
is to assign values to the members numRows and numCols. I see you have a commented out SetSize method. I would assume you don't want to do use it because you think the rows and columns shouldn't change after creation.
In that case, your matrixStore should be created as pointers:
MatrixType* matrixStore[10];
Now instead of doing:
MatrixType matrix = MatrixType(rows, cols);
matrixStore[index] = matrix;
You do this:
matrixStore[index] = new MatrixType(rows, cols);
And just use:
matrixStore[index]->StoreItem(value, i, j);
or whatever you want to do with it.
In the end, you just call:
delete matrixStore[index];
For all matrices you have used "new" on. The best way to do this is to assign them to nullptr in the beginning.
MatrixType* matrixStore[10];
for ( unsigned int i = 0; i < 10; ++i )
{
matrixStore[i] = nullptr;
}
And in the end:
for ( unsigned int i = 0; i < 10; ++i )
{
if (nullptr != matrixStore[i])
{
delete matrixStore[i];
}
}
In short, matrixStore stores object values, not pointers (as you expect).
//Create Result Matrix and a pointer variable to it
MatrixType pResultMatrix = MatrixType(rows, cols);
matrixStore[resultIndex] = pResultMatrix;
Seems you expect to store pointer variable, but it's not true. MatrixType value is stored in matrixStore.
The result of this is incorrect matrixStore filling (code from your link):
MatrixType matrix = MatrixType(rows, cols);
matrixStore[index] = matrix;
cout << "Address of matrixStore[index] : " << &matrixStore[index] << endl;
cout << "Address of new matrix is : " << &matrix << endl;
int value;
for (int i = 0; i < rows; i++) {
cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++) {
cin >> value;
matrix.StoreItem(value, i, j);
}
}
All changes to matrix are lost as matrixStore contains old copy of matrix object.
Solution:
The proper declaration of matrixStore to keep pointers is:
MatrixType* matrixStore[10]
In case you would like to operate with MatrixType value objects you need to copy objects into matrixStore all time you've modified them.
Looking at the code in http://pastebin.com/aj2eEGVS
It seems to me you are not initializing the matrix in matrixStore. You are filling the local variable matrix.
MatrixType matrix = MatrixType(rows, cols); // LOCAL VARIABLE
**matrixStore[index] = matrix;** // you are copying the uninitialized matrix
// ...
int value;
for (int i = 0; i < rows; i++) {
cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++) {
cin >> value;
matrix.StoreItem(value, i, j); // LOCAL VARIABLE
}
}
cout << endl;
//Print matrix so the use can see
matrix.PrintMatrix(); // PRINT LOCAL VARIABLE
I'm wondering if something is wrong with my code especially the vector implementation?
Well,I was just exposed to the use of vector yesterday by people here.
In my college,I only learnt array.So,the usage of vector is kinda new to me.
To my understanding,vector is basically a dynamic array.-Correct me if I were wrong
Well,so lets go with my code.I got the following error: "Vector subscript out of range" after inputting n value.
EDIT:Fixed my earlier issue.Thanks to #quantdev .Now I noticed that my values aren't sorted.
#include<iostream>
#include<vector>
using namespace std;
//Function prototype
void Insertion_sort(vector<int> AR, int n);
void random_store(int val, vector<int> &aVec);
int main()
{
int nvalue;
vector<int> int_vector;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
random_store(nvalue, int_vector);//pass user input into random() function
system("pause");
return 0;
}
void random_store(int val, vector<int> &aVec)//store randomly generated value
{
int num;//represent random integer output
for (int i = 0; i < val; i++)
{
aVec.push_back(rand() % val + 1);//push each generated value into vector
}
Insertion_sort(aVec,val);//Pass the vector into a function to perform sorting
cout << " \n The sorted array is as follows \n ";
for (int i = 1; i <= val; i++)//Print sorted array
{
cout << " \n Element " << i << " : " << aVec[i] << endl;//will loop from aVec 1st array till n value
}
}
void Insertion_sort(vector<int> AR, int n)//insertion sort function
{
int j, val;//iterate through entire list
for (int i = 1; i < n; i++)
{
val = AR[i];
j = i - 1;
while (j >= 0 && AR[j] > val){
AR[j + 1] = AR[j];
j = j - 1;
}
AR[j + 1] = val;
}
} // end of insertion sort function
The problem is that your vector contains val values, so indexes are in [0, val-1], but within this loop :
for (int i = 1; i <= val; i++)
The last iteration will try to access the element at index val+1, which is out of bounds (it also misses the first element, at index 0)
Change it to :
for (int i = 0; i < val; i++)
And since indexes are of type std::size_t :
for (std::size_t i = 0; i < val; i++)
Note:
Your sort function takes a vector by value, sorting a copy of the vector. You probably want to pass by reference instead :
void Insertion_sort(vector<int>& AR, int n)
Trying to learn C++ and working through a simple exercise on arrays.
Basically, I've created a multidimensional array and I want to create a function that prints out the values.
The commented for-loop within Main() works fine, but when I try to turn that for-loop into a function, it doesn't work and for the life of me, I cannot see why.
#include <iostream>
using namespace std;
void printArray(int theArray[], int numberOfRows, int numberOfColumns);
int main()
{
int sally[2][3] = {{2,3,4},{8,9,10}};
printArray(sally,2,3);
// for(int rows = 0; rows < 2; rows++){
// for(int columns = 0; columns < 3; columns++){
// cout << sally[rows][columns] << " ";
// }
// cout << endl;
// }
}
void printArray(int theArray[], int numberOfRows, int numberOfColumns){
for(int x = 0; x < numberOfRows; x++){
for(int y = 0; y < numberOfColumns; y++){
cout << theArray[x][y] << " ";
}
cout << endl;
}
}
C++ inherits its syntax from C, and tries hard to maintain backward compatibility where the syntax matches. So passing arrays works just like C: the length information is lost.
However, C++ does provide a way to automatically pass the length information, using a reference (no backward compatibility concerns, C has no references):
template<int numberOfRows, int numberOfColumns>
void printArray(int (&theArray)[numberOfRows][numberOfColumns])
{
for(int x = 0; x < numberOfRows; x++){
for(int y = 0; y < numberOfColumns; y++){
cout << theArray[x][y] << " ";
}
cout << endl;
}
}
Demonstration: http://ideone.com/MrYKz
Here's a variation that avoids the complicated array reference syntax: http://ideone.com/GVkxk
If the size is dynamic, you can't use either template version. You just need to know that C and C++ store array content in row-major order.
Code which works with variable size: http://ideone.com/kjHiR
Since theArray is multidimensional, you should specify the bounds of all its dimensions in the function prototype (except the first one):
void printArray(int theArray[][3], int numberOfRows, int numberOfColumns);
I'm aware of the date of this post, but just for completeness and perhaps for future reference, the following is another solution. Although C++ offers many standard-library facilities (see std::vector or std::array) that makes programmer life easier in cases like this compared to the built-in array intrinsic low-level concepts, if you need anyway to call your printArray like so:
printArray(sally, 2, 3);
you may redefine the function this way:
void printArray(int* theArray, int numberOfRows, int numberOfColumns){
for(int x = 0; x < numberOfRows; x++){
for(int y = 0; y < numberOfColumns; y++){
cout << theArray[x * numberOfColumns + y] << " ";
}
cout << endl;
}
}
In particular, note the first argument and the subscript operation:
the function takes a pointer, so you pass the name of the multidimensional array which also is the address to its first element.
within the subscript operation (theArray[x * numberOfColumns + y]) we access the sequential element thinking about the multidimensional array as an unique row array.
If you pass array as argument you must specify the size of dimensions except for the first dim. Compiler needs those to calculate the offset of each element in the array. Say you may let printArray like
void printArray(int theArray[][3], int numberOfRows, int numberOfColumns){
for(int x = 0; x < numberOfRows; x++){
for(int y = 0; y < numberOfColumns; y++){
cout << theArray[x][y] << " ";
}
cout << endl;
}
}
I'm trying to figure out how to pass 2D array, which is constructed dynamically to a function.
I know that number of columns must be specified, but it my case it depends on user input.
Are there any workarounds?
Example:
// Some function
void function(matrix[i][j]) {
// do stuff
}
// Main function
int N;
cout << "Size: ";
cin >> N;
int matrix[N][N];
for (int i=0;i<N;i++) { //
for (int j=0;j<N;j++) {
cin >> matrix[N][N];
}
}
sort(matrix);
You get the idea :)
If you're on C++, the reasonable options are to:
use boost::multi_array (recommended), or
make your own 2D array class. Well, you don't have to, but encapsulating 2D array logic in a class is useful and makes the code clean.
Manual 2D array indexing would look like this:
void func(int* arrayData, int arrayWidth) {
// element (x,y) is under arrayData[x + y*arrayWidth]
}
But seriously, either wrap this with a class or enjoy that Boost already has that class ready for you. Indexing this manually is tiresome and makes the code more unclean and error-prone.
edit
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html says that C99 has one more solution for you:
void func(int len, int array[len][len]) {
// notice how the first parameter is used in the definition of second parameter
}
Should also work in C++ compilers, but I haven't ever used this approach.
In C++, the compiler can figure out the size, since it's part of the type. Won't work with dynamically sized matrices though.
template<size_t N, size_t M>
void function(int (&matrix)[N][M])
{
// do stuff
}
EDIT: In GCC only, which is required for your code defining the array, you can pass variable-length arrays directly:
void func(int N, int matrix[N][N])
{
//do stuff
}
See the gcc documentation
/*******************************************************\
* *
* I am not claiming to be an expert, but I think I know *
* a solution to this one. Try using a Vector Container *
* instead of an array. Here is an example below: *
* *
* Load the target file with a Multiplication Table *
* *
* *
\*******************************************************/
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
std::string user_file;
int user_size = 2;
void array_maker(int user_size, std::string user_file);
int main () {
std::cout << "Enter the name of the file for your data: ";
std::cin >> user_file;
std::cout << std::endl;
std::cout << "Enter the size for your Multiplication Table: ";
std::cin >> user_size;
// Create the users Multiplication data
array_maker(user_size, user_file);
return (0);
}
void array_maker(int user_size, std::string user_file)
{
// Open file to write data & add it to end of file
std::ofstream target_file(user_file,std::ios::out | std::ios::app);
// Declare the vector to use as a runtime sized array
std::vector<std::vector<int>> main_array;
// Initialize the size of the vector array
main_array.resize(user_size+1); // Outer Dimension
for (int i=0; i <= user_size; ++i) // Inner Dimension
{
main_array[i].resize(user_size+1);
}
for (int i=0; i<=user_size; ++i)
{
for (int j=0; j<=user_size; ++j)
{
main_array[i][j] = i * j;
// output line to current record in file
target_file << i << "*"
<< j << "="
<< main_array[i][j] << " "
<< "EOR" // End of Record
<< std::endl;
} // Close Inner For
} // Close Outer For
// close file
target_file.close();
} // Close array_maker function
You can do
void function (int** __matrix, int32_t __row, int32_t __column)
__row - max rows
__column - max columns.
You will need those params to find out the limits of the array.
Just add another parametrs to your function - row_number and column_number. Arrays are not object in C++ so they don't store any additional information about themselfs.
If you pass in the array identifier (as a pointer to a pointer) you will need to use pointer arithmetic:
void function(int** matrix, int num_rows, int num_cols) {
Assert(matrix!=NULL && *matrix!=NULL && num_rows>0 && num_cols>0);
for(int i=0; i<num_rows; i++) {
for(int j=0; j<num_cols; j++) {
// cannot index using [] like matrix[i][j]
// use pointer arithmetic instead like:
// *(matrix + i*num_cols + j)
}
}
}
to pass multi dimensional arays into method the compiler needs to know the depth of each field, so one solution is to use templates and call method in a normal way and the compiler will guess the size of each field.
template <size_t m>
void method(int M[][m])
{
for(int i=0; i<m; ++i)
for(int j=0; j<m; ++j)
{
// do funny stuff with M[i][j]
}
}
int main()
{
int M[5][5] = { {1,0,1,1,0}, {0,1,1,1,0}, {1,1,1,1,1}, {1,0,1,1,1}, {1,1,1,1,1} };
method(M);
// also you can call with method<5>(M)
// if you have different sizes for each dimension try passing them in args
return 0;
}
int r, c
int *matrix = new int[r,c];
for (int i = 0; i < r; i++)
{
/*cout << "Enter data" << endl;*/
for (int j = 0; j < c; j++)
{
cin >> matrix[i,j];
}
}
void function(int &matrix[][] )