Lets say I have two functions, for example I'm trying to create a fixed size array.
1 2 3 4 5 6 7 8 9 10 11 12
int Recursion::fixedSize(int arraySize){
int arrayMax=1000;
cout<<"What size do you want your array? (choose a number less than 1000): ";
cin>>arraySize;
if(arraySize>arrayMax){
cout<<"Your array size is too big!";
exit(0);
}
else{
return arraySize;
}
}
Now I need to use the value of arraySize to enter a certain amount of integers. For example this code:
1 2 3 4 5 6 7 8 9
int* Recursion::arrayValue(int arraySize){
int value;
for(int i=0;i<arraySize;i++){
cout<<"Enter the "<< (i+1) <<" value of the array: ";
cin>> value;
arr[i]=value;
}
return arr;
}
But when I put cout<<arraySize; at the end of the function caller in my main, it gets the prints out 0.
Also I was wondering how I can link arraySize to my int arr[], which is private.
1 2 3 4 5 6 7
class Recursion{
private:
int arr[];
public:
Recursion();
int fixedSize(int arraySize);
int*arrayValue(int arraySize);
Sorry if this is a lot of questions but thank you all in advance.
In my main or in my header file?
This is the code for my main:
1 2 3 4 5 6 7 8 9 10 11 12 13
main(){
int n=10;
int m=9;
int arraySize;
int large;
int start=0;
int endpoint=10;
int k;
cout<<"This program will work with recursions and arrays"<<endl;
Recursion a;
a.fixedSize(arraySize);
a.arrayValue(arraySize);
cout<<endl<<"**************************************"<<endl;
That^ would return an error because its out of bounds(size=arraySize-1) and it also would return only the last variable, *which would not be very ideal.
For more info on how to return arrays, see here: http://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm
javascript:editbox2.editSend()