Array with randome numbers

I are having some problems, with fixing my code here.

I want the code to write in a randome number into the elements of the array.

Can anyone give me some guidlines how to get this code to work? :)

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

using namespace std;

int main()
{
	array<int, 7> lottorad;// Ett heltalsfält som lagrar lottoraden
	bool lika = false;

	for (int i = 1; i < lottorad.size(); i++) {
		lottorad[i] = rand() % 35 + 1; // randomise a number betwin 1-35
		for (int j = 1; j < lottorad.size(); j++) { // loops throu if the number has been drawn before
			if (lottorad[i] == lottorad[j]) {
				lika = true;
			}
			if (lika == true) { // if the number has been drawn, go back and reroll
				i--;
				lika = false;

			}

			else { // if the number has not been drawn write it out.
				cout << lottorad[i] << endl;
			}

		}
	}
	return 0;
}
Are you obliged to use an array?
It would be much easier to use a set.
A set has only unique elements which are automatically sorted.
> how to get this code to work?
¿what's its problem?


I'll suggest you to move your "loops throu if the number has been drawn before" to a function.
1
2
3
4
5
6
7
for (int K=0; K<lottorad.size(); ++K){ //index goes from 0 to n-1
   int number;
   do{
      number = rand()%35 + 1;
   } while( is_present(number, lottorad) );
   lottorad[K] = number;
}
then you may realize the mistake on line 18.

Also, you need to seed the rng http://www.cplusplus.com/reference/cstdlib/srand/
hmm, explain more about this "set" never heard of it before :o how do i write it?

The thing is, they gave me a code with aloth of errors and misstake. i have fixed it up to this. but i am kinda stuck, and i think i dont have to use an array i can use whatever i want. just make it as simple and good as i can :)
1
2
3
4
5
6
7
    set<int> lottorad;
    srand (time(NULL));
    while (lottorad.size() < 7)
    {
      int num = rand() % 35 + 1;
      lottorad.insert(num);
    }

You need
#include <set>
#include <time.h>

Some info about the set class:
http://www.cplusplus.com/reference/set/set/
Someting like this?

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

using namespace std;

int is_present(int i, int array) {
	bool lika = false;
	for (int j = 1; j < lottorad.size(); j++) { // Loopar igenom de tidigare slumpade tal och kontrollerar om det aktuella talet redan har dragits
		if (lottorad[i] == lottorad[j]) {
			lika = true;
		}
		if (lika == true) { // Om det aktuella talet redan dragits så minskas i med 1. Man går alltså tillbaka ett steg i loopen och slumpar ett nytt tal
			i--;
			lika = false;

		}

		else { // Om det aktuella talet inte hade dragits tidigare så skrivs det ut
			cout << lottorad[i] << endl;
		}
	}
}

int main()
{
	array<int, 7> lottorad;// Ett heltalsfält som lagrar lottoraden
	bool lika = false;

	for (int K = 0; K<lottorad.size(); ++K) { //index goes from 0 to n-1
		int number;
		do {
			number = rand() % 35 + 1;
		} while (is_present(number, lottorad));
		lottorad[K] = number;
	}
	return 0;
}
Last edited on
yes, it is a custom function.
It would check the array to see if the number is already there. Basically what your loop 14--28 is doing incorrectly.

perhap is_member() may be a better name.


> A set has only unique elements which are automatically sorted.
the last part may be a problem
How do i make the function take an array?
I changed it abit now, but it only gives me 0 in the print out? why wont it randomise?
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 <array>
#include <time.h>
static int lottorad[7];
using namespace std;

void randomNumber(int pos)
{
	srand(time(NULL));
	lottorad[pos] = rand() % 35 + 1;
	for (int j = 1; j < lottorad[7]; j++) { // Loopar igenom de tidigare slumpade tal och kontrollerar om det aktuella talet redan har dragits
		if (lottorad[pos] == lottorad[j]) {
			pos--;
		}

		else{ // Om det aktuella talet inte hade dragits tidigare så skrivs det ut
			cout << lottorad[pos] << endl;
		}
	}
}

int main()
{

	{
		for (int a = 0; a < lottorad[7]; a++)
		{
			randomNumber(a);
		}
	}
	cout << lottorad[1] << endl;
	cout << lottorad[7] << endl;
}
Last edited on
> How do i make the function take an array?
The same way that you would make it take any variable
bool is_present(int value, const std::array<int, 7> &array);
Or you could use pointers
1
2
bool is_present(int value, const int *array, int size);
is_present(number, lottorad.data(), lottorad.size()); //calling 


Your function should not do any output to the console.


for (int j = 1; j < lottorad[7]; j++)
¿what are you doing there?
¿why does `j' start in 1?
¿what are you accessing the 8th element of the array? (which doesn't exist)

if you say that your function returns a value, then there should be a return statement.


Also, you should only seed the rng once.
Last edited on
Topic archived. No new replies allowed.