I am trying to create a random matrix where the user inputs the size and then the program outputs a square matrix that size but the values of the matrix are all random. I am using newmat to accomplish this. Here is my code, can you tell me whats wrong? I am told that C is not initialized which I can see but I am not sure what to do to fix this.
I need n to be an input from the user though.... It can't be equal to something ahead of time can it? I could initialize it to zero and then have the user replace it, is that what you mean? Am I even correct in my confusion here?
I'm also using part of a code provided to me by my professor and that's why I have C and A as pointers (I think that's what they are...). I tried having them be set as matrices but then I get a problem with:
If they need to be dynamically allocated at runtime then you can use the (hackish)
1 2 3 4 5 6 7 8 9
double **c = newdouble*[n]; //allocate an array of double pointers of n length, and store a pointer to it in c
for( int i = 0; i < n; i++ )
c[i] = newdouble[n]; //allocate an array of doubles of n length and store a pointer to it in c[i]
//Now initialize c with values.
//...
//Later, when your done with c, clear up memory...
for( int i = 0; i < n; i++ )
delete[] c[i];
delete[] c;
There are better ways, but this may be the easiest that works for you.