13 Replies - 250 Views - Last Post: 20 September 2019 - 08:37 AM Rate Topic: -----

#1 Deadweight77   User is offline

  • New D.I.C Head

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

random numbers with dynamic array

Posted 20 September 2019 - 07:12 AM

I've recently been introduced to dynamic arrays and need a little help. I know some of it,but need help with a random number generator. Normally this would be easy, but given it is dynamic array I may need help here. The output is way off and I wish to know why and how I can fix it.

#include <iostream>
#include<ctime>
using namespace std;
int* getRandomNumbers(int *num);
int main()
{
	int* num;
	int size;
	cout << "How big should the array be?" ;
	cin >> size;
	num = new int[size];
	for (int i = 0;i < size;i++)
	{
		getRandomNumbers(num);
		cout << num;
	}
	
	return 0;
}

int* getRandomNumbers(int *num)
{
	srand(time(0));
	*num = rand() % 50 + 1;
	return num;
}


This post has been edited by Deadweight77: 20 September 2019 - 07:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: random numbers with dynamic array

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 07:14 AM

What part do you need help with?
In what way is the data off?
What data are you getting? What should you be getting?
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: random numbers with dynamic array

Posted 20 September 2019 - 07:21 AM

I need help with the random generator part and returning the values back to the main function. The data I get now are all random numbers that are way past the size of the array I request. I should be able to enter in the size of the array, so lets say I want 4, and I should be able to get 4 random numbers between 1-50. I'm not sure what I'm doing wrong with the generator.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 07:34 AM

A few things things.

First - you have an array, you better initialize it.

Second it's not prudent to just say 'print array'. Perhaps try iterating over each element to print.

Third - why is your loop not inside the 'get random numbers'? If you want each item in the array to be a value then it's best to step through each index!
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: random numbers with dynamic array

Posted 20 September 2019 - 07:57 AM

so should it look a little more like this?

#include <iostream>
#include<ctime>
using namespace std;
int* getRandomNumbers(int *num,int & size);
int main()
{
	int* num;
	int size;
	cout << "How big should the array be?" ;
	cin >> size;
	num = new int[size];
	
	
	return 0;
}

int* getRandomNumbers(int *num,int &size)
{
	srand(time(0));
	for (int i = 0;i < size;i++)
	{
		//insert random generator

	}
	return num;
}


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:01 AM

What happens when you run it/debug it?

Does it make more sense to iterate over the individual elements and assign values, or to blanketly assume to assign a value to the array pointer?
Was This Post Helpful? 0
  • +
  • -

#7 Deadweight77   User is offline

  • New D.I.C Head

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

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:10 AM

For line 22 I inserted this:

*num = rand() % 50 + 1;



and got random numbers disproportionate to the array size I wanted with some letters in the mix for the output.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:15 AM

Read this line out loud. It says "Assign a random value to the pointer 'num'".
*num = rand() % 50 + 1;


It doesn't say "assign a random value to an indexed value in the pointer 'num'".

How would you get individual elements in an array?
Was This Post Helpful? 0
  • +
  • -

#9 Deadweight77   User is offline

  • New D.I.C Head

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

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:20 AM

so it would be more like this?
num[i] = rand() % 50 + 1;


Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:21 AM

What happens when you try that?
Was This Post Helpful? 0
  • +
  • -

#11 Deadweight77   User is offline

  • New D.I.C Head

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

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:25 AM

same result I'm afraid
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:27 AM

Post your current code.
Was This Post Helpful? 0
  • +
  • -

#13 Deadweight77   User is offline

  • New D.I.C Head

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

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:32 AM

Here. Forgive me because I still somewhat of a novice at this. I'm basically learning latin here since I tried all these different things prior to coming here for help.

#include <iostream>
#include<ctime>
using namespace std;
int* getRandomNumbers(int *num,int & size);
int main()
{
	int* num;
	int size;
	cout << "How big should the array be?" ;
	cin >> size;
	num = new int[size];
	cout << num;
	
	return 0;
}

int* getRandomNumbers(int *num,int &size)
{
	srand(time(0));
	for (int i = 0;i < size;i++)
	{
		num[i] = rand() % 50 + 1;

	}
	return num;
}


Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15317
  • View blog
  • Posts: 61,418
  • Joined: 12-June 08

Re: random numbers with dynamic array

Posted 20 September 2019 - 08:37 AM

Where in there are you calling your method? Perhaps try it WITHOUT the confusing complexity of a method first.

Also, if one assigns values in an array by using indexes would it not follow that's how you would go about printing values in an array? By utilizing index..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1