# Size of an array

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:52 PM GMT

## Question

QUESTION: Hello Zlatko; Is there any function in C++ that can calculates the summation of some values? I have some constants like A(1),A(2)...A(n) that n and A(j) for j=1..n , will be introduced at the beginning of the program. But n may be different for various data that each time I'll give them to the program. Now, I want to build an array like this: int array[b]; b=sum of A(j) | j=1..n How can I do that?

As far as I know, there is no function to sum the contents of an array. It is a simple matter to build one using a loop, as I'm sure you know.

int sum(int* pArray, int size) {

```  int result = 0;
for(int ix = 0; ix < size; ++ix) result += pArray[ix];
return result;
```

}

The idea of making an array based on a size determined at runtime is not possible with the current C++ standard. I think it will be in the next release of the language. Until then, you need to do this:

int b = sum(A, n); int* myArray = new int[b];

I hope that helps you out.

---------- FOLLOW-UP ----------

QUESTION: Dear Zlatko; Thanks for your guidance. But as far as I know the size of an array must be a constant integer number. I think "b" in the above structure that you write it, is a variable. Isn't it?