# Adding integers inside of an array

Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 11-09-2012
randyjohnson
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
• 11-09-2012
Sorinx
Yeah that's fine if you want to do a check to make sure value entered has 12 chars
• 11-09-2012
randyjohnson
Camel-man, is that using an array though? That's the only option I have with the assignment
• 11-09-2012
Sorinx
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); }```
• 11-09-2012
camel-man
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.
• 11-09-2012
randyjohnson
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
• 11-09-2012
camel-man
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.
• 11-09-2012
Sorinx
Yeah either way works unless you hit enter before 12 chars need to put a few more lines in there lol
• 11-09-2012
camel-man
Im assuming the user knows exactly what to enter ;) (which is never the case in reality :D)
• 11-09-2012
randyjohnson
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-09-2012
Sorinx
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");```
• 11-09-2012
randyjohnson
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)
• 11-09-2012
Sorinx
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
• 11-10-2012
christop
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.
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123