How to return an array from a function
Hello,
what I want my program to do is the following:
1. generate an array of N random numbers in a function
2. return that array to the main function
3. display the contents of this array (later ofcourse I want to do other things with it, but this is the easiest thing for now)
What I have for code is:
Code:
/*
Header: iostream
Reason: Input/Output stream
Header: stdlib
Reason: For functions rand and srand
Header: time.h
Reason: For function time, and for data type time_t
*/
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int randomRange(int min, int max,int number);
int main()
{
int list2=randomRange(10,20,10);
for(int x=0;x<10;x++){
cout<<list2[x]<<endl;
}
}
int randomRange(int min, int max, int number)
{
//Make the seed for the random function
time_t seed;
time(&seed);
srand((unsigned int) seed);
int list[number];
//get a random number from range min-max
int rnumber;
for(int i=0;i < number;i++){
rnumber=rand() % (max-min) + min;
list[i]=rnumber;
cout<<rnumber<<endl;
}
return list;
}
Could you please tell me what I'm doing wrong? =)