array fill

Started by
2 comments, last by bentaberry 14 years, 1 month ago
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.
Advertisement
The problem is that your 'array' variable in 'main' is not an array.
"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];

taytay
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

This topic is closed to new replies.

Advertisement