# Thread: Sum of an array with pointers

1. ## Sum of an array with pointers

Hi, I've been trying to sort this out all day, and it's driving me crazy because I'm pretty sure its just a matter of straightening out the functions and pointers, but I can't make anything work.

I need to calculate the sum of the first 20 Fibonacci numbers with the fibonacci numbers stored in an array. This is where I'm at, any help would be very much appreciated. Thanks!

Code:
```#include <stdio.h>

int arraySum(int *array, const int n)
{
int sum = 0;
int * const arrayEnd = array + n;

for ( ; array < arrayEnd; ++array )
sum +=*array;

return sum;
}

int main(void)
{
int arraySum (int *array, const int n);
int i, numFibs= 20;

int fibonacci[numFibs];

fibonacci[0] = 0;
fibonacci[1] = 1;

for (i = 2; i < numFibs; ++i)
fibonacci[i] = fibonacci[i-2] + fibonacci[i-1];

printf("Their sum is %i\n ", arraySum (fibonacci, 20) );

return 0;
}```

2. You can't do this:
Code:
```int numFibs= 20;
int fibonacci[numFibs];```
because the array's dimension needs to be static and reserved at compile time, and you are making it dynamic to be allocated at runtime. Won't work in C.

3. Originally Posted by Dino
You can't do this:
Code:
```int numFibs= 20;
int fibonacci[numFibs];```
because the array's dimension needs to be static and reserved at compile time, and you are making it dynamic to be allocated at runtime. Won't work in C.
It will work in C99, although I don't really see the point.

Quzah.

4. Any other clues? Sorry I am terrible at this. Tried just plugging fibonacci[20] in, but that clearly doesn't work...

5. What does "doesn't work" mean?

Quzah.

Code:
```int numFibs= 20;
int fibonacci[numFibs];```
Does it make sense to separate the two? So the fibonacci is it's own function?

kind of like this, except not because that still doesn't work

Code:
```#include <stdio.h>

int arraySum(int *array, const int n)
{
int sum = 0;
int * const arrayEnd = array + n;

for ( ; array < arrayEnd; ++array )
sum +=*array;

return sum;
}
int fibonacci(int *fib)
{
int i;
fib[20];
fib [0] = 0;
fib [1] = 1;

for(i = 2; i < 20; ++i)
fib[i] = fib[i-2] + fib[i-1];
return 0;
}

int main(void)
{
int arraySum (int *array, const int n);
int fibonacci(int *fib);
int fib[20];

printf("Their sum is %i\n ", arraySum (fib, 20) );

return 0;
}```

7. Just for the record, this is for a continuing ed class - there is no grade involved. I work as a designer and just want a better understanding of programming languages.

8. Originally Posted by Jamathorn

Code:
```int numFibs= 20;
int fibonacci[numFibs];```
Does it make sense to separate the two? So the fibonacci is it's own function?

kind of like this, except not because that still doesn't work

Code:
```#include <stdio.h>

int arraySum(int *array, const int n)
{
int sum = 0;
int * const arrayEnd = array + n;

for ( ; array < arrayEnd; ++array )
sum +=*array;

return sum;
}
int fibonacci(int *fib)
{
int i;
fib[20];
fib [0] = 0;
fib [1] = 1;

for(i = 2; i < 20; ++i)
fib[i] = fib[i-2] + fib[i-1];
return 0;
}

int main(void)
{
int arraySum (int *array, const int n);
int fibonacci(int *fib);
int fib[20];

printf("Their sum is %i\n ", arraySum (fib, 20) );

return 0;
}```
Where are you calling the fibonacci function? You dont need to have another array in your function arraySum, it can be done without it too, simply running the loop till 'n'.

9. When I run the first posting, I get 10945. What's the answer supposed to be?

I didn't even try the second posting because the fibonacci() function is defined but not called and fib[] is passed uninitialized to arraySum().

10. Wow, you're right that does work...I'm not sure why I couldn't make that happen before. Now I feel extra ridicules, though slightly less broken. Thanks!