Like Tree3Likes

Adding integers inside of an array

This is a discussion on Adding integers inside of an array within the C Programming forums, part of the General Programming Boards category; Haha well this way would be a lot easier and outside the box for him...

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Haha well this way would be a lot easier and outside the box for him

  2. #17
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Okay this is what I have set up so far
    Code:
    int a[12], i, sumEven, sumOdd, total, entry;
    printf("Type in one number of the UPC code and press enter");
    for(i=0;i<=12;i++){
    scanf("%d\n", &a[i]);}
    for(i=0;i<=12; i+=2)
    sumEven+=a[i];
    sumOdd+=a[i+1];
    
    total = sumOdd*3 + sumEven;
    Do I have it placed all in the correct order?

  3. #18
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Lol hold on I'll fix it up a bit. I didn't try and compile. Make sure you realize that arrays start at value 0. so you doing <=12 isn't going to store 12 values.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int a[12], i, sumEven, sumOdd, total, entry;
        printf("Type in one number of the UPC code and press enter");
            for(i=0;i<12;i++){
                scanf("%d\n", &a[i]);
                            }
            
            
            for(i=0;i<12; i+=2){
                sumEven+=a[i];
                sumOdd+=a[i+1];
                                }
    total = sumOdd*3 + sumEven;
    
    }
    I hope you understand how this and why it works. For loop sets i to zero then checks if its less than 12. If it's less than 12, it scans the value entered by user into the array at i. then incremements i til i<12 is no longer viable. The second part I incremented by 2 because you wanted to add the even and odd numbers. so a[i]+2 will always be even where as a[i+1] will always be odd and it adds it every increment. make sure to use braces when you are doing for loops you need to localize loops
    Last edited by Sorinx; 11-09-2012 at 08:12 PM.

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Haha man you are seriously awesome. I'm sorry for being bothersome I just really want to get this done. Thank you for all the help

  5. #20
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Dude no problem, I can't figure out my code right now personally haha, and I knew what it was like trying to do ........ when I started 3 months ago and everyone would give some hard ass answer and it would make your head spin. Figured I'd keep it simple
    randyjohnson likes this.

  6. #21
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    637
    FYI SumEven and SumOdd should be initialized to 0 in the beginning so you are not left with some outrageously high number.

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah good call I am doing 2 things at once

  8. #23
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    637
    Not to nitpick here but also you want to switch the indexes of sumEven and Sumodd
    Code:
       sumEven+=a[i];//<---- this is first number of array meaning 1 in reality. 1= odd
                sumOdd+=a[i+1];// <--- second number 2= even :D
    switch to

    Code:
       sumOdd+=a[i];
                sumEven+=a[i+1];

  9. #24
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Seems like I should get some sleep missing stupid things

  10. #25
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Apparently the 12th digit in the array is not actually supposed to be counted. Is there a simple adjustment I can make so that this is the case with the code that's already written?

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    lol yeah man just change it to i<11. i is the counter, that's why it's set to i = 0. Then the condition is checked for i<11. Then it does the instructions, then it adds 2 to i, and rechecks the counter. so on and so on. (IDK why it deletes certain words out, the forum that is)
    Last edited by Sorinx; 11-09-2012 at 08:53 PM.

  12. #27
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    I tried that and had it print out the total for both sumEven and sumOdd and it's still counting the last digit in the array even with this
    Code:
    for(i=0;i<11; i+=2)

  13. #28
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Post what your actual code looks like now, from that, that isn't possible

  14. #29
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    637
    Code:
    #include <stdio.h>
     
    int main(void)
    {
          int i, sumEven=0, sumOdd=0, total=0;
          char UPC[13];
    	printf("Enter in Your UPC code\n");
    	scanf("%12s", UPC);
    
    	for(i=0;i<12;i++)
    	{
    		if(i%2==0)
    		sumOdd+=UPC[i]-'0';
    		else
    		sumEven+=UPC[i]-'0';
    	}
        
    	total = (sumOdd*3) + sumEven;
    	printf("Your total is %d\n", total);
    
            return 0;
    }
    This is what I was getting at, notice the user friendliness. Here you can just enter in the UPC code like a normal UPC code, rather than the tedious enter after enter.

  15. #30
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    0 to 11 = 12, 0 to 10 = 11

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. Adding Long Integers!
    By alireza beygi in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 06:11 AM
  3. adding 16bit and 32 bit integers
    By kris_perry2k in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 08:49 AM
  4. Adding integers to a string
    By Kyoto Oshiro in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 07:01 AM
  5. C++ help!!!!! Adding integers
    By PR1MO in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 05:40 PM

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