1. ## Adding integers inside of an array

I'm horrible at C and I have to write a program for class that's due tomorrow that has to do with UPC codes. Basically I'm supposed to prompt the user for a 12 digit UPC and then check to see if it's valid or not. To do this I have to store the UPC in an array.

Now, I know how to check if the UPC is valid on paper. You have to add all the odd positions (for example, if the UPC is 268473619543 you would add 2, 8, 7, 6, 9 and 4 together) and then multiply that number by 3. Then I have to add all the even positions in the UPC and add that to the odd position's sum.

Anyway, what my problem is I can't figure out how to do this in code. How would I add the numbers while they are stored into an array?

2. for(i=0;i<arraysize; i+=2)
sumEven+=array[i];
sumOdd+=array[i+1];

3. Get one thing to work at a time. first prompt the user and scan in a UPC code. Post some code, It will be easier for people here to help you when you have something down already. What you might want to do it use the UPC code as a string. That way you can traverse through the numbers and add up the odd/even positions individually.

4. Just for loop like I did to go through every value in the array starting from 0. In this case you want to add every even and odd number. Easiest way is to do i and i+1 to represent even and odd. i+=2 will increment it by 2 every time it goes through loop, then add the values as it goes through to a variable you wish. If it is multi dimensional just nest another for

5. Sorinx, I think what Randy is trying to do is add up the actual odd/even numbers of the single UPC code, not the array itself.

6. Wait that works... he just has to store 1 UPC code in a int array...Then add the numbers in the odd positions... i is just the pointer to the address that stores the value that he entered for the UPC code

7. Yes I have to store 1 UPC code into an array and then add the odd/even positions in the array. Will the for loop work for that?

8. Yeah man lol, idk what he's talking about. Use for loops to read/store in arrays unless you have predetermined values. Just follow the template I set up

9. @ Sorinx Post some pseudocode on here and explain to me what you mean. If you enter a UPC code into an array and you use your technique then the UPC array should not be an int array. It should be a string. Unless he is hard coding the int array like such
Code:
`UPC[]={1,4,7,9,3........};`
I fail to see how your code will work... Unless the user inputs the UPC number one integer at a time. Randy, are you supposed to enter the UPC code like this 1 enter 4 enter 5 enter..... Or, is it supposed to be one number like 487233455... enter.

10. Lol or you could take the UPC code from the user 1 value at a time with a scanf in a print statement incrementing the pointer every time a number goes through. Boom done, and quite on the level that he is. No need to get into strings malloc and rewind with a beginner

11. Sorinx, how would I go about doing that method? Not to be a pain in the ass, but I'm pretty lost here. I've only been working with C for a few weeks now.

12. Yes, I could see that working. I was assuming that the number was entered like a number should be entered, which is all on one line.

13. it's cool you have your array size so make a
Code:
```printf("enter in one number of UPC code hit enter enter in next or whatever you want");
for(i=0;i<12;i++){
scanf("%d\n", &arrayName[i]);}```
now your compares and ........ will be a lot easier because like you said you are a few weeks in now, it gets a bit complicated lol

dont

14. Originally Posted by camel-man
Yes, I could see that working. I was assuming that the number was entered like a number should be entered, which is all on one line.
And you can't figure out how to move the pointer inside the cell? lol I have been only doing C for 3 months...

15. No, I know how to "move the pointer inside of the cell" just fine :P