I'm having trouble creating a simple program. The program requests the number of input values to collect (which is used to create an array of the appropriate size), collects the values, and computes the product of all numbers whose position is >= 1. The problem is that no matter what size is specified (i.e., which controls the size of the created array) the size of array is always reported as 4. For example, if 10 inputs are collected an array of size 10 is created but checking the size of the array results in 4.
This is the code:
int main() {
double *aNumberArray = 0;
aNumberArray = askNumbers();
int position = 0;
position = sizeof(aNumberArray);
for (int i = 0; i < position; i++) {
cout << aNumberArray[i] << endl;
}
cout << "The product of your numbers is " << productOfArray(aNumberArray, position) << endl;
delete[] aNumberArray;
return 0;
}
double *askNumbers() {
int size;
double *numbers ;
cout << "Product of the numbers which positions are n >= 1" << endl;
cout << "How many numbers would you like to multiply? ";
cin >> size;
numbers = new double[size];
cout << "Type in your numbers: " << endl;
for (int i = 0; i < size; i++) {
cout << "Number " << i + 1 << ": ";
cin >> numbers[i];
}
return numbers;
}
double productOfArray(const double anArray[], int size) {
const double number = 1;
double product = 0;
if (size >= 1) {
product = anArray[size] * (productOfArray(anArray, size - 1));
}
else if (size == 0)
return number;
return product;
}