Help with a 2-D random array

There is a problem on my maths tripos to simulate 100 random samples of size n=2 from a uniform distribution on [0,1].

To do this I wrote a program in C++ to assign a rand % 100 + 1 to a 2 * 100 matrix, with row 1 and row 2 denoting n=2 and the columns denoting each sample. Then have a 100 * 1 vector = average of entries in rows of 2 * 100 matrix / 100.

Anyway I have been trying for ages and the answers I get are rather absurd for a what should be a random number. My program gives numbers bigger than 100 (let alone 1), but mainly 0s.

While this is a big hassle to read, as I cannot figure out where the exact mistake is, I cannot really do anything but post the entire code. If anyone has a better method from scratch or just to point out a mistake that would be brilliant! I am very new to this and did this all myself with a few pints of guinness, so it probably doesn't make any sense:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	int c;
	cin >> c;
	srand ( time(0));
	int Array [2][100];
	float v [100];
	int n = 1;
	int m = 1;
	while (m<=100)
	{
		m++;
		
		while (n<=2)
		{
			n++;
			Array[n-1][m-1] = ((rand() % 100) + 1);	
		}
		
	}
	n=1;
	
	while (n<=100)
	{
		n++;
		v [n-1] = (  (Array[0][n-1] + Array[1][n-1])  /  100   );
	}
	n=1;
	cout << "\n\n";
	
	while (n<=100)
	{
		n++;
		cout << v [n-1] << "  ";
	}
	cout << "\n\n\n";
		return 0;
}
Last edited on
To be honest, I have no idea what you're trying to do.
If I understand correctly: you want 100 random numbers between [0,1] and, to get this, you draw 2x100 random numbers between [1,100], then sum the pairs and divide by 100?

If so:
a) If you're adding up two [1,100] variables and divide by 100, then the result will be [0.02,2], not [0,1]. You should draw by "rand()%51" if you want the result in 'v' to be [0,1].

b) I'm unsure why you're getting high numbers, but maybe it's because of the space between 'v' and '[n-1]'. Maybe you're actually outputting 'v' and '[n-1]', not 'v[n-1]'. However, that would probably give errors, so I doubt that's the problem...

c) You'd be best to replace those while loops with for loops. It gets rid of the excess variables and it's easier to read. Alternatively, initializing 'n' and 'm' at '0' instead of '1' and replacing your while condition with (n<100) would make it much more readable. Don't forget to replace [n-1] with [n] everywhere too then.

d) I'm guessing your "cin >> c;" should have been at the bottom of the code to prevent the window from closing?
1st, remember to use correct code tag to put your code.. the correct code tag has the line number to help in disccussion.. i think u are using program output tag for this...
2nd, I dont quite understand what you try to achieve in your program but obviously there are some problem in the program..
The main problem will be at
1
2
3
4
5
6
7
8
9
10
11
12
13
        int n = 1;
	int m = 1;
	while (m<=100)
	{
		m++;
		
		while (n<=2)
		{
			n++;
			Array[n-1][m-1] = ((rand() % 100) + 1);	
		}
		
	}

This part of program does not allow the continuous loop of the value n.. that's mean the while(n<=2) loop only occur for the 1st loop of while (m<=100).
After that, every loop of while (m<=100) does not go through while(n<=2) anymore.. making this meaningless..
Another problem is at many part of the program, you initialize a variable with number 1, then let it loop through while(x<=100) as example:
1
2
3
4
5
6
int n=1;
while (n<=100)
	{
		n++;
		cout << v [n-1] << "  ";
	}

This will access the value of array v from v[1] to v[100] but the actual array of size 100 should be from v[0] to v[99]...
Topic archived. No new replies allowed.