Sign in to follow this  

array fill

This topic is 2845 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
"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

This topic is 2845 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this