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;
}
8
  • 6
    That's a clue that sizeof doesn't do what you think it does and it's time to do some research. Commented Mar 18, 2015 at 5:17
  • sizeof does not return an int variable saying how many elements are in the array? Commented Mar 18, 2015 at 5: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. Commented Mar 18, 2015 at 5:19
  • So how could I determine the size of the array? Do another function for that specific task? Commented Mar 18, 2015 at 5: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. Commented Mar 18, 2015 at 5:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I see, so how can I get the size of the array? Make another function to determine the size?
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!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.