Hello, I'm trying to create a matrix that dynamically defines the rows and columns.. Now, everything is fine, apart from when I include a function to output the 2D array..

The error:
Matrix.cpp:20: error: invalid types ‘double[int]’ for array subscript
...

The code is:

Matrix.cpp

#include <iostream>
#include "Matrix.h"

using namespace std; 

Matrix::Matrix(){};

Matrix::Matrix(double M, double N)
{
    rows = M;
    columns = N;
    matrix = new double[(unsigned int)rows*(unsigned int)columns];

}

double Matrix::getRead()
{
    for (int i=0, j=0; (i < rows, j < columns); i++, j++)
    {
        cout << matrix[i][j];   
    }    
}

Matrix.h

#ifndef _Matrix_h
#define _Matrix_h
#include <vector>

using namespace std;
class Matrix
{
    public:
    
        Matrix(); // Constructor 
        Matrix(const double M, const double N);
    
        double getRead();

    protected:
    
    double* matrix;
    
    int rows;
    int columns;
};


#endif

Any ideas? Thanks =)

matrix is a 1d matrix, not a 2d matrix. You can keep it as a 1d matrix if you want to but that means you have to calculate the row and column index yourself.

This is one of the rare times I could start the i counter with 1 instead of 0 so that the calculation (i-1) does not become a negative number. And this needs two loops, not one.

for (int i=1; i<= rows; i++)
{
    for(int j = 0; j < colums; j++)
        cout << matrix[((i-1) * rows) + j];   
}

Edited 4 Years Ago by Ancient Dragon: n/a

Comments
Great help :) - Thanks

Sorry, I'm confused..

matrix = new double[(unsigned int)rows*(unsigned int)columns];

Is 2D right?

e.g.

matrix = new double[10][20];

Also, would I set the values the same way (i.e. using the example loop you gave?)

Thanks :)

Sorry, I'm confused..

matrix = new double[(unsigned int)rows*(unsigned int)columns];

Is 2D right?

e.g.

matrix = new double[10][20];

No. Get rid of all the unnecessary stuff in the line and you get matrix = new double[rows*columns]; Where's the 2D in that? Count the square brackets...

A 2d array might look something like this: Note that it has 2 asterisks, not just one.

double **array = new double[rows*sizeof(double*)]; // allocate the rows number of pointers
for(int i = 0; i < rows; i++)
   array[i] = new double[cols]; // allocate columns for each row

Edited 4 Years Ago by Ancient Dragon: n/a

Hey, when I try your example.. It just throws up errors :(!

#include <iostream>

using namespace std;

void changeArray(int rows, int columns)
{
	double **array = new double[rows*sizeof(double*)];
	for(int i = 0; i < rows; i++)
	   array[i] = new double[columns]; // allocate columns for each row
	
}
int main(int argc, char *argv[]) {
	
	double rows = 10;
	double columns = 30;
	
	double *array;	
}

Untitled.cpp: In function 'void changeArray(int, int)':
Untitled.cpp:7: error: cannot convert 'double*' to 'double**' in initialization

Any ideas?

line 7 was slightly incorrect. It should have been this:
double **array = new double**[rows*sizeof(double*)];

Also in the code you posted, changeArray() is not called from main()

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hey thank you so much for your reply.. BUT

#include <iostream>

using namespace std;

void changeArray(int rows, int columns)
{
	double **array = new double**[rows*sizeof(double*)]; 
	for(int i = 0; i < rows; i++)
	   array[i] = new double[columns]; // allocate columns for each row
	
}
int main(int argc, char *argv[]) {
	
	double rows = 10;
	double columns = 30;
	
	double *array; 
	
	changeArray(rows, columns);
}

Error:
Untitled.cpp: In function 'void changeArray(int, int)':
Untitled.cpp:7: error: cannot convert 'double***' to 'double**' in initialization

Am I completely missing the point? I hate 2D arrays :(!

Edited 4 Years Ago by phorce: n/a

You need to learn how to recognize and correct errors.

double** changeArray(int rows, int columns)
{
	double **array = new double*[rows*sizeof(double*)]; 
	for(int i = 0; i < rows; i++)
	   array[i] = new double[columns]; // allocate columns for each row
	return array;	
}
int main(int argc, char *argv[]) {
	
	int rows = 10;
	int columns = 30;
	
	double** array = changeArray(rows, columns);
}

I look so stupid now ;) sorry my brain has just been fried with all this!

Thank you so much for your help!

This question has already been answered. Start a new discussion instead.