5 Replies - 249 Views - Last Post: 20 October 2019 - 02:23 PM Rate Topic: -----

#1 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Set with random number generator

Posted 18 October 2019 - 08:19 AM

I've begun to do a new program, but I've hit a roadblock and need help. I'm trying to enter the size of a set that has a random number generator in it. So when I enter in the size should be 3 I should get 3 random numbers, but that doesn't happen. The code will look a bit off since I wanted to see if the function actually worked before progressing further.

#include <iostream>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std;

class Set {

public:


	//default constructor
	Set();
	//add element to set
	void addElement(int element);
	//remove element from set
	void removeElement(int element);
	//check for membership
	bool isMember(int element);
	//set union, modifies curremtn set
	void Union(Set s);
	//set difference modifiers current set
	void difference(Set s);
	//size of set
	int size();
	//get element i
	int getElement(int i);

private:

	//binary search for element, returns index
	bool search(int element, int& index);
	//set members
	//int elements[maxElements];
	//next empty position in elements
	int next;
};

Set::Set()
{
	
}

int Set::size()
{
	srand(time(0));
	set<int> s{ rand() % 25 + 1 };
	int S_size;
	cout << "Enter in the size of the set" << endl;
	cin >> S_size;
	for (int i = 0;i < S_size;i++)
	{
		cout << s.size();
	}
	return s.size();
}

int main(set<int>&)
{
	Set enter;
	enter.size();
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Set with random number generator

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7133
  • View blog
  • Posts: 24,231
  • Joined: 05-May 12

Re: Set with random number generator

Posted 18 October 2019 - 09:05 AM

At what point do you believe that S_size should have magically wet the size of the set<int> s that you declared and initialized on line 47?
Was This Post Helpful? 0
  • +
  • -

#3 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: Set with random number generator

Posted 18 October 2019 - 09:33 AM

I thought I could randomly generate in the set itself and see if I could've set the size of the set in a for loop. What do you suggest I change?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7133
  • View blog
  • Posts: 24,231
  • Joined: 05-May 12

Re: Set with random number generator

Posted 18 October 2019 - 10:47 AM

Alas,
set<int> s{ rand() % 25 + 1 };


is not like in mathematics (or Python) where you can say something in curly braces and it defines a function for generating values in a set.

What is actually happening is something like this:
int x = rand() % 25 + 1;
set<int> s { x };


Only one value is put into the set.

What you'll want to do is start off with an empty set, and then as your for loop iterates, add one random number into the set for each iteration.
Was This Post Helpful? 0
  • +
  • -

#5 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: Set with random number generator

Posted 20 October 2019 - 12:17 PM

I changed it to become this:

#include <iostream>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std;

class Set {

public:


	//default constructor
	Set();
	//add element to set
	void addElement(int element);
	//remove element from set
	void removeElement(int element);
	//check for membership
	bool isMember(int element);
	//set union, modifies curremtn set
	void Union(Set s);
	//set difference modifiers current set
	void difference(Set s);
	//size of set
	int size();
	//get element i
	int getElement(int i);

private:

	//binary search for element, returns index
	bool search(int element, int& index);
	//set members
	//int elements[maxElements];
	//next empty position in elements
	int next;

};

Set::Set()
{
	
}

int Set::size()
{
	srand(time(0));
	int x = rand() % 25 + 1;
	set<int> s{x};
	set<int>::iterator it = s.begin();//iterator "it" starts at the begining of the set 
	int S_size;
	cout << "Enter in the size of the set" << endl;
	cin >> S_size;
	for (int i = 0;i < S_size;i++)
	{
		cout << *it << " ";
	}
	return s.size();
}

int main(set<int>&)
{
	Set enter;
	enter.size();
	
}


It works somewhat because the number generator works, but each random number is the same. Is there a way to make sure that each number is unique within the number generator?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7133
  • View blog
  • Posts: 24,231
  • Joined: 05-May 12

Re: Set with random number generator

Posted 20 October 2019 - 02:23 PM

You are not advancing your iterator.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1