generating random numbers in arrays

Hi, I am a total beginner here. I am trying to write a program where I have to generate a 6x6 arrays (36 in all) , and then automatically generate a random integers and assign them into all 36 arrays. And then print it out in a 6x6

The range is from 1 to 9999, and that all 36 numbers has to be unique to each other (no duplicates in the 36 numbers).

The following is what I have so far, so I really need some assistance to help me with what's wrong and how to rectify it.
(I am using for loop to go through all the numbers)

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int array [6][6]; //6x6 array

int generateRand() { //create random nums
	return rand() %10000; //random numbers from 1 to 9999
}

int main()

void generateArray(int array[6][6])

{
	srand(time(NULL));
	for (int i = 0;i<6;i++)

	{

		for (int j = 0;j<6;j++)

		{

			array[i][j] = generateRand();
			cout << array[i][j];

		}

		cout << endl;

	}
return 0;
}
Last edited on
your main function does not have a body:
int main() {}

Note that the main function is the function that's called when starting the program.

So in your main function you will want to call the function generateArray

You have to declare generateArray before main so move main to the bottom of your code.

furthermore you pass a parameter to generateArray with the same name as your global variable so it won't modify the global, but the local array.
Since you pass by value you don't save the array anywhere.

Also: \j is no escape character
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int array [6][6]; //6x6 array

int generateRand() { //create random nums
	return rand() %10000; //random numbers from 1 to 9999
}


void generateArray()
{
    srand(time(NULL));
    for (int i = 0;i<6;i++)
    {
        for (int j = 0;j<6;j++)
        {
            cout << " ";
            array[i][j] = generateRand();
            cout << array[i][j];
	}

    cout << endl;
    }
    return 0;
}

int main() 
{
    generateArray();
}


you should take a look at functions again: http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Topic archived. No new replies allowed.