How to let a function return an array

E

erictham

#include <iostream>
#include <cmath>
using namespace std;

// implements a pseudo RNG generator by the linear congruential
generator
const int n1=100;
double *LRNG(int, int, int);
int main()
{
double *random;
int a,c,n ;
cout << "Please enter the seed for a and the constant for c";
cin >> a >> c;
cout << "How many random numbers do you want to generate?";
cin >> n;
random = LRNG(a, c, n);
for (int i=1; i<n1;i++)
{
cout << random << endl;
}
// How do u return a array from a function?
return 0;
}

double *LRNG(int a, int c, int seed, int n) {
double M;
//const int n1= 100;
double m0[n1], r[n1];
m0[1]= 1;
// How do u assign a variable size to an array??
M = pow(2,32);
for (int i=1;i<n1;i++)
{
r = fmod(a * m0+c,M);
}
return r;
}
Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

LinearRNG.exe - 2 error(s), 0 warning(s)

I am lost as to what to do next. thanks..
 
V

Victor Bazarov

[...]
Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

You should get rid of both the warning and the error. First, do not
return a local variable. Better pass the appropriately sized array
into the function so it can fill it with required values. Second,
compare the declaration (prototype) of the 'LRNG' function and the
definition. Should become obvious why symbol is unresolved.

V
 
G

Gianni Mariani

#include <iostream>
#include <cmath>
using namespace std;

// implements a pseudo RNG generator by the linear congruential
generator
const int n1=100;
double *LRNG(int, int, int);

double *LRNG(int, int, int); <- takes 3 ints
int main()
{
double *random;
int a,c,n ;
cout << "Please enter the seed for a and the constant for c";
cin >> a >> c;
cout << "How many random numbers do you want to generate?";
cin >> n;
random = LRNG(a, c, n);
for (int i=1; i<n1;i++)
{
cout << random << endl;
}
// How do u return a array from a function?
return 0;
}

double *LRNG(int a, int c, int seed, int n) {


double *LRNG(int a, int c, int seed, int n) <- takes 4 ints

The linker is saying it can't find LRNG with 3 ints ! This is because
you have not provided one.
double M;
//const int n1= 100;
double m0[n1], r[n1];

r[n1]; <- returning this means it will be destructed before
the user can use it.

try
std::vector<double> r(n1);

returning this will make a copy, if you don't want to make a copy, pass
it as a parameter.
m0[1]= 1;
// How do u assign a variable size to an array??
M = pow(2,32);
for (int i=1;i<n1;i++)
{
r = fmod(a * m0+c,M);
}
return r;
}


Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

LinearRNG.exe - 2 error(s), 0 warning(s)

I am lost as to what to do next. thanks..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top