adding elements in array

This is a discussion on adding elements in array within the C Programming forums, part of the General Programming Boards category; I'm making a program that will add all the values in an array to get a total value, I wrote ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    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.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    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

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    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.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    Thumbs up

    Quote Originally Posted by Leojeen View Post
    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!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,313
    It looks like I'm going to beat Elisia to saying it this time:

    main must return int, not void.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting all elements of an array to zero
    By kolistivra in forum C Programming
    Replies: 9
    Last Post: 08-29-2006, 02:42 PM
  2. Adding elements to array?
    By INFERNO2K in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2005, 12:56 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21