Note that there are some explanatory texts on larger screens.

plurals
  1. POC - Array sum function constantly returning zero
    primarykey
    data
    text
    <p>I'm trying to write a a program that lets the user generate anywhere from 1-75 Fibonacci numbers, print them, and then add all of those values together to get to over all sum. I had no problem generating the numbers and printing them, but for some reason I'm having a <em>lot</em> more trouble with the sum function. For some reason it only returns 0...and after staring at this for as many hours as I have I'm at a loss. Any and all help would be greatly appreciated. My code is below:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; //function to calculate fibonacci number 1-75 (user specified) int fibonacci(int * array, int size) { int i; unsigned long long int Fibonacci[size]; //generating the array to save the Fibonacci values Fibonacci[0] = 0; //by definition Fibonacci[1] = 1; //by definition for (i = 2; i &lt; size; i++) { Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1]; //equation to calculate the numbers } for (i = 0; i &lt; size; i++) { printf("%llu\t", Fibonacci[i]); //printing out the numbers } printf("\n"); return size; } int fibonacciSum(int * array, int size) { int i; int sum = 0; for (i = 0; i &lt; size; i++) { sum += array[i]; } return sum; } int main(void) { int size; printf("How many Fibonacci numbers do you want to generate (between 1 and 75)?\n"); scanf("%i", &amp;size); int * FibonacciNumbers = (int*) calloc(size,sizeof(int)); if (size &lt; 1 || size &gt; 75) { printf("Bad number!"); return 1; } fibonacci(FibonacciNumbers, size); printf("The sum of the Fibonacci numbers is: %d\n", fibonacciSum(FibonacciNumbers, size)); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload