Dynamic memory and arrays! help

Hi

I'm tryig to perform some matrix calculations with ++, but i want to define the matrix and it's dimensions as an array within the program using dynamic memory, rather than explicitly at the beginning of the program.

it seems to work in the sense that it counts the refernce to elements of the matrix correctly, but when i test i cannot retrieve the values from the array.

I don't know why? PLease can you ahve a look:

the error occurs at the test stage in bold



// Matrix calc.

#include <iostream>
#include <new>
#include <string>
#include <sstream>

using namespace std;
int main ()

{

int i,n,a=0,b=0,x,m,t;
int * Matrix;
string y;

cout << "How many Matrices do you want to manipulate? ";
cin >> i;

Matrix = new (nothrow) int[i];

{

for (x=1; x<i+1; x++)

{
cout << "\n For Matrix " << x << " please enter the dimensions \n";
cout << "\n N is: ";
cin >> n;
cout << "\n M is: ";
cin >> m;
int Matrix[x] [n][m];

for (a = 0; a < n ; a++)

{
for (b = 0; b < m ; b++)
{
cout << "\n Enter value Matrix " << x << " reference [" << a << b <<"] ";
cin >> Matrix[x] [a];
}
}
}

//test for storage correct

cout << "\n check value? number and ref: \n";

[b] cout << Matrix[1] [0][0];






}
system ("PAUSE");
return 0;

}

Last edited on
You have defined Matrix = new (nothrow) int[i]; but you don't assign any values to it.

Within some braces {} you define
int Matrix[x] [n][m];

and assign it some values. Shortly after it goes out of scope and the Matrix that you new'ed above is printed, which has no values.

EDIT:
oops!
Having had a bit more time to look at this, erm.. it dosn't even compile....

Myself not being any kind of math guru, can you show a working version that dosn't use dynamic memory? I could help from there...
Last edited on
Topic archived. No new replies allowed.