Hey guys
I'm trying to generate a Matrix with random numbers between 0 and 1, but i'm not getting anywhere... this is what i got:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
int n,m;
srand(time(NULL));
cout << "Geben Sie die Dimension ihrer Matrix ein" << endl;
cout << "n =";
cin >> n;
cout << "m =";
cin >> m;
int **mat = new int*[n];
for (int i=0; i<m; i++) {
mat[i]=new int[n];
}
for (int row=0; row<m; row++) {
for (int column=0; column<n-1; column++){
float rdm;
rdm = (float)rand()/(float)RAND_MAX;
mat[row][column] = (float)rdm;
}
}
for (int row2=0; row2<m; row2++) {
for (int column2=0; column2<n-1; column2++){
cout << mat[row2][column2] << ", ";
}
cout << mat[row2][n-1] << endl;
}
return 0;
}
|
unfortunately, for a 3x3 Matrix, the only Output i'm getting is:
0,0,0
0,0,0
0,0,0
any help will be appreciated
cheers
Last edited on
Your matrix is storing numbers of type int so the decimal part is discarded when you assign a floating point value.
When you generate the random numbers you use n-1 when it should be n.
thanks a lot, works like a charm