# adding elements in array

Printable View

• 12-30-2008
reb221
adding elements in array
I'm making a program that will add all the values in an array to get a total value, I wrote out my program on paper and it should run just fine, but instead of the final variable of the array just getting a total value of all integers, it gets the first value of the array. Here's my program:
Code:
Code:

```#include <stdio.h> void main()  // bcc32 -IC:\Borland\bcc55\include -LC:\Borland\bcc55\Lib register2.c  to compile {                int        a[1000], b, c=0, d[1000], i=0,  f[1000];  // these elements are for later         float        e;         printf("Please enter the price of the item and hit 0 to quit:\$");         scanf("%d", &a[i]);                                while(a[i]!=0)                 {                                         if(i==0)                         {                         i++;                         }                                     printf("Please enter the price of the item and hit 0 to quit:\$");                 scanf("%d", &a[i]);                // filling array values                                                                         if(a[i]!=0)                         {                         a[i+1]=a[i]+a[i-1];                         i+2;        // adding array values                         }                                 }                                                 printf("\nThe total is \$%d", a[i-1]); }                                                      // end program```
The second if statement is where I'm having trouble.
When I write it out it should run like this: i=0, a[0]=1, i=1, a[1]+a[1-1]=a[1+1]
i=3, a[3]=2, a[3+1]=a[3]+a[2] etc.
• 12-30-2008
Leojeen
Seems like you're trying to implement a Fibonacci sequence!
Anyway, that does "i+2" mean? If you want to increment i by 2 the way to do it is
Code:

`i = i + 2`
• 12-30-2008
laserlight
A few things that will help you:
• Indent your code properly. This will allow you to more clearly see the structure of your loops and if statements.
• Only declare the variables that you need now. If you need variables later, add them in later.
• Although on a technicality it is possible to argue the contrary, the main function is supposed to return an int, to declare it as such, and return 0 at the end.
• 12-30-2008
rpet
The second time your program reaches the loop header, i is 3, and a[3] is undefined at this point.
By pure chance a[3] is 0 and the execution stops.
• 12-30-2008
reb221
Quote:

Originally Posted by Leojeen
Seems like you're trying to implement a Fibonacci sequence!
Anyway, that does "i+2" mean? If you want to increment i by 2 the way to do it is
Code:

`i = i + 2`

Thanks, that fixed it!
• 12-30-2008
iMalc
It looks like I'm going to beat Elisia to saying it this time:

main must return int, not void.
• 12-30-2008
laserlight
Quote:

Originally Posted by iMalc
It looks like I'm going to beat Elisia to saying it this time:

main must return int, not void.

Unfortunately, you lost to me, as per post #3 :p