I am trying to do something like this but I'm not sure what is the best way. What am I doing wrong? I have also tried changing double v[] to double *v
#include <iostream>
#include <random>
using namespace std;
void PopulateVector(double v[])
{
delete v;
v = new double[5];
v[0] = 1;
v[1] = 1;
v[2] = 1;
v[3] = 1;
v[4] = 2;
}
int main()
{
double *test = new double[1];
PopulateVector(test);
for (int i = 0; i < 5; i++)
{
cout << test[i] << endl;
}
}
Based on the good comments. I made some modifications. This version works but i still wish the void PopulateVector(double *v) or PopulateVector(double v[]) worked.
#include <iostream>
#include <random>
using namespace std;
double* PopulateVector()
{
double *v = new double[5];
v[0] = 1;
v[1] = 1;
v[2] = 1;
v[3] = 1;
v[4] = 2;
return v;
}
int main()
{
double *test = new double[1];
delete[]test; // Do I need this?
test = PopulateVector();
double *test2 = new double[1];
test2 = PopulateVector();
for (int i = 0; i < 5; i++)
{
cout << test[i] << endl;
}
}