Matrix Generating - i don't get it

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.
So, this
1
2
3
4
int **mat = new int*[n];
	for (int i=0; i<m; i++) {
		mat[i]=new int[n];
	}

should be this?
1
2
3
4
int **mat = new int*[n];
	for (int i=0; i<m; i++) {
		mat[i]=new float[n];
	}
ok so i was able to fix this problem by doing this:

1
2
3
4
float **mat = new float*[n];
	for (int i=0; i<m; i++) {
		mat[i]=new float[n];
	}


now, for the last row it only spits out zeroes

0.424708, 0.0144198, 0
0.291924, 0.357273, 0
0.701961, 0.508485, 0

how come?
When you generate the random numbers you use n-1 when it should be n.
thanks a lot, works like a charm
Topic archived. No new replies allowed.