multidimensional array with random numbers

Hello everyone, I am having a bit of a problem with this program. I want it to ask the user for a number of rows and number of columns. I then want the program to create a multidimensional array using these dimensions. Next, I want it to populate the array with random numbers from 1 to 1000...Here is what I have so far:

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
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
	int i;
	int j;
	int n1;
	int n2;
	
	cout << "Enter number or rows: " << endl;
	cin >> n1;
	cout << "Enter number of columns " << endl;
	cin >> n2;
	
	srand(time(0));	
	
	double A[n1][n2];
	
	int d = rand() % 1000;

	for( i=0; i<n1; i++)
	{
		for( j=0; j<n2; j++)
			A[i][j] = double(i+j);
	}
	
	for( i=0; i<n1; i++)
	{
		for( j=0; j<n2; j++)
			A[i][j] = d;
			cout <<" "<< A[i][j] << endl; 
	}
	
	return 0;
}


Can someone tell me where I've made the mistake?
1. You can't make dynamic-size arrays like that in C++. You need to look up how to use new and delete, under dynamic memory in the tutorial.

2. On line 27, what are you doing? It seems like you are just populating the array with some values that are later erased by the assignment on line 33.

3. Speaking of line 33, the d variable is a single random number. That means all the elements in your array will have the same random number. Try assigning A[i][j] to rand()%1000 directly instead.
Thank you for the advice, Zhuge. I will research dynamic memory and arrays a bit more and try to come up with something more sensible..
Topic archived. No new replies allowed.