Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

array fill

This topic is 3059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well I am trying to fill an array with random numbers from 1 to 100 using pointers.
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void RandomArrayFill(int* array,int size);

int main()
{
	srand(time(0));

	int size,array=0;

	cout << "Enter the size of an array to create: ";
	cin >> size;
	cout << endl;
	
	cout << "Creating array and filling it with random numbers..." << endl;
	
	cout << "Array = {" <<  "}" << endl;
	
//	RandomArrayFill(array,size);

	return 0;

}

void RandomArrayFill(int* array,int size)
{
	for(int i=1;i<=size;i++)
	{
	*array=rand()%100;
	}
}

the commented line is where I am having the problem.I am getting a compiler error.

Share this post


Link to post
Share on other sites
Advertisement
"int array" is not an array, just an integer.

to create a dynamic array you need to create a pointer.

int *array

then allocate some memory for it

array = new int[size];

Share this post


Link to post
Share on other sites
for

array = new int[size];

dont forget to delete[] the array pointer afterwards and set the pointer to null to avoid memory leaks.

Kind Regards
David

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!