#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;
}
}
array fill
well I am trying to fill an array with random numbers from 1 to 100 using pointers.
the commented line is where I am having the problem.I am getting a compiler error.
"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];
to create a dynamic array you need to create a pointer.
int *array
then allocate some memory for it
array = new int[size];
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
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
Popular Topics
Advertisement