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:
The second if statement is where I'm having trouble.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
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.



LinkBack URL
About LinkBacks



