I am trying to create a random matrix. I am using Newmat10 and its appropriate libraries. I am not sure how to create a random matrix though. Here is what I have been trying:
Matrix create_random_matrix(Matrix D, int n,int k)
{
int i, j;
int T;
Matrix A(n,n);
cout << endl;
cout << "---------------";
for (i = 1; i < n; i++) {
cout << endl;;
for (j = 1; j < n; j++){
T=rand()% 100 +1;
A(i,j) = T;
cout << A << "\t";
}
}
cout<<A<<endl;
return A;
cout << endl;
cout << "---------------";
}
I ask the user to input n and k in the main code so it can be any size but it will always be square. Thank you in advance for any help you can provide.
Looks like it works to me. 2 ; in line 11, early return that stops 21 and 22 from happening, but nothing fatal. What's the problem? Is there an error, or is the matrix not random enough?
It doesn't return good values. I get something like 41, -41.00000, 25694839729834819234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000000 and
-25694839729834819234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000000
Oh sorry. Well, the reason is obviously because you're trying to print an entire array as a single number which makes no sense. Another problem is that I have no idea how your Matrix class works.
cout << A << "\t"; This is doubtfully the right way to return the specific destination
cout << A[i][j] << "\t"; If A does return an array this is the way to go
If this fails try to find a function or variable in your class that handles the array. cout << A.getarray(i,j) << "\t";
Perhaps..?