Thread: Adding integers inside of an array

  1. #31
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    All I have right now is what you gave me earlier pretty much
    Code:
     
    #include <stdio.h>
     
    int main(void)
    {
    int a[12], i, sumEven, sumOdd, total;
        printf("Type in one number of the UPC code and press enter");
            for(i=0;i<12;i++){
                scanf("%d\n", &a[i]);
                            }
             
            sumEven = 0;
            sumOdd = 0;
            for(i=0;i<11; i+=2){
                sumOdd+=a[i];
                sumEven+=a[i+1];
                                }
    total = sumOdd*3 + sumEven;
    printf("odd sum is %d\n", sumOdd);
    printf("even sum is %d\n", sumEven);
     printf("total is %d\n", total);
    
    return 0;
    }
    I am using 681131700085 as an example UPC. The even sum should only be 10 but it keeps printing out as 15

  2. #32
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah that's fine if you want to do a check to make sure value entered has 12 chars

  3. #33
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Camel-man, is that using an array though? That's the only option I have with the assignment

  4. #34
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    He's using a char array and using a string then modulus

    Code:
    #include <stdio.h>
    
    int main()
    {
    int a[12], i, sumEven=0, sumOdd=0, total=0;
        printf("Type in one number of the UPC code and press enter");
            for(i=0;i<11;i++){
                scanf("%d\n", &a[i]);
                            }
            
            
            for(i=0;i<10; i+=2){
                sumOdd+=a[i];
                sumEven+=a[i+1];
                                }
    total = sumOdd*3 + sumEven;
    printf("total: %d", total);
    }

  5. #35
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Yes it is using an array, what does your professor require of you? That it be an integer array, char array, or just an array in general? If so then yes it uses a char array, and should work fine.

  6. #36
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    In class he was giving us examples with int arrays but the assignment itself just says array and doesn't specify. So I assume it would be fine

  7. #37
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Well, if he does not specifically direct what array he wants then, I would imagine this would work.
    P.S. If you do not want the last digit to be calculated then just change the i<12 to i<11.

  8. #38
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah either way works unless you hit enter before 12 chars need to put a few more lines in there lol

  9. #39
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Im assuming the user knows exactly what to enter (which is never the case in reality )

  10. #40
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    This is the last part of the problem. If you guys can help at all you will be my freaking heroes: If the last digit in the total is 0, then the check digit is 0; otherwise subtract the last digit from 10 to calculate the check digit.If the computed check digit matches the twelfth digit of the UPC, the UPC is assumed to be correct; otherwise it is incorrect.

    I know I have to use a few if statements but what confuses me is how to actually check a specific number like the last digit in the total or checking to see if it matches the last in the array.

  11. #41
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Depends on which way you chose to do it really. Strings have a lot of different functions but if you did it my way you would just have to do Array[11] would be the 12th value... so something like

    Code:
    if (a[11]==0)
    checkdigit=0;
    else
    checkdigit=10-a[11];
    
    if checkdigit==a[11];
    printf("UPC is correct");
    Last edited by Sorinx; 11-09-2012 at 10:32 PM. Reason: lol as I explain array[11] I make it 12

  12. #42
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    You have to do it with the total though like if the UPC was 681131700085, after calculating the total comes to 85. If the last digit is 0 then that's the check digit but if it's not (which it's not because the last digit is 5) then you subtract the last digit from 10 and that's the check digit. Then after all that you have to see if the check digit is equal to the last digit in the UPC. I just have no idea how to write this in code.

    (It's a mandatory class, by the way, otherwise I wouldn't be taking it. I think it's pretty clear that I'm not exactly a master of coding)

  13. #43
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    OH you know what modulus is man? it takes the number divides it by something then returns the remainder. so... %10 would = 5 just rewrite it the same way I did but instead of the array just set checkdigit=total%10; then do your if statements

  14. #44
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    What I think everyone here has missed so far is that this:
    Code:
            for(i=0;i<11; i+=2){
                sumOdd+=a[i];
                sumEven+=a[i+1];
                                }
    gives exactly the same results as this:
    Code:
            for(i=0;i<12; i+=2){
                sumOdd+=a[i];
                sumEven+=a[i+1];
                                }
    Think about the possible values of i inside the loop in each case: 0, 2, 4, 6, 8, 10. In both cases you're adding up 6 "odd" digits and 6 "even" digits.

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, 07:11 AM
  3. adding 16bit and 32 bit integers
    By kris_perry2k in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 09:49 AM
  4. Adding integers to a string
    By Kyoto Oshiro in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 08:01 AM
  5. C++ help!!!!! Adding integers
    By PR1MO in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 05:40 PM