Silvestrini
Silvestrini

Reputation: 445

How to determine the size of an array?

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

Upvotes: 0

Views: 105

Answers (2)

paxdiablo
paxdiablo

Reputation: 882426

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.

Upvotes: 1

Gary Kaizer
Gary Kaizer

Reputation: 284

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

Upvotes: 1

Related Questions