0

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;
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
Silvestrini
  • 445
  • 4
  • 19
  • 6
    That's a clue that `sizeof` doesn't do what you think it does and it's time to do some research. – chris Mar 18 '15 at 05:17
  • sizeof does not return an int variable saying how many elements are in the array? – Silvestrini Mar 18 '15 at 05:18
  • No, and it doesn't given an array instead of a pointer, either. It doesn't even result in an `int`, but a `size_t`. – chris Mar 18 '15 at 05:19
  • So how could I determine the size of the array? Do another function for that specific task? – Silvestrini Mar 18 '15 at 05:21
  • 1
    If this is `c++` then I'd recommend not using a c-style array and instead use `std::vector`, its size is a property of the class. – James Adkison Mar 18 '15 at 05:23
  • Thing is I have not yet gotten into that topic. This is for an assignment and I can't use what I have not been taught yet. – Silvestrini Mar 18 '15 at 05:25
  • Then do what it does manually. `struct CrappyVector {int *data; int numElems;};` – chris Mar 18 '15 at 05:29
  • possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – sashoalm Mar 18 '15 at 06:13

2 Answers2

1
double *aNumberArray = 0;
position = sizeof(aNumberArray);

The aNumberArray variable is a pointer rather than an array. Hence its size is the size of a pointer (four in your case).

A pointer carries no size information for an underlying array of objects, you can only get the size of the pointer or the size of the one object pointer to.

If you want to pass the size back, you could use something like:

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];

    //get numbers, blah blah blah

    return numbers;
}

and call it with:

double *aNumberArray = 0;
int position;
aNumberArray = askNumbers(position);

You can see the effect with the following code:

#include <iostream>
#include <iomanip>

double *getArray (int &sz) {
    std::cout << "What size? ";
    std::cin >> sz;
    double *array = new double[sz];
    for (int i = 0; i < sz; i++)
        array[i] = 3.14159 * i;
    return array;

}

int main(int argc, char *argv[]){
    int count;
    double *xyzzy = getArray(count);
    for (int i = 0; i < count; i++)
        std::cout << std::fixed << std::setw(6) << xyzzy[i] << '\n';
    delete [] xyzzy;
    return 0;
}

When you compile and run that, you can see that the size is indeed passed back, using the method suggested:

What size? 4
0.000000
3.141590
6.283180
9.424770

But you might also want to consider becoming a "real" C++ developer rather than a hybrid C+ developer (a strange breed that never appears to have fully made the transition). You can do this by using the collections that come with the standard library, such as a vector which does carry the size along with it:

#include <iostream>
#include <iomanip>
#include <vector>

std::vector<double> getArray () {
    int sz;
    std::cout << "What size? ";
    std::cin >> sz;
    std::vector<double> vect = std::vector<double>(sz);
    for (int i = 0; i < sz; i++)
        vect[i] = 3.14159 * i;
    return vect;

}

int main(int argc, char *argv[]){
    std::vector<double> xyzzy = getArray();
    for (int i = 0; i < xyzzy.size(); i++)
        std::cout << std::fixed << std::setw(6) << xyzzy[i] << '\n';
    return 0;
}

It doesn't look that much simpler but it'll become so as your program becomes larger, and knowing how to use it will make your life a lot easier in the long run.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I see, so how can I get the size of the array? Make another function to determine the size? – Silvestrini Mar 18 '15 at 05:20
  • That helped out. Thanks for the solution and the guide. Will be sure to check the Vectors out as it probably makes arrays and pointers easier to deal with! – Silvestrini Mar 18 '15 at 05:33
1

Here is a quick tutorial about getting array size: http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/

Gary Kaizer
  • 274
  • 1
  • 7