# 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. Haha well this way would be a lot easier and outside the box for him

2. 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. 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

4. 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. 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

6. FYI SumEven and SumOdd should be initialized to 0 in the beginning so you are not left with some outrageously high number.

7. Yeah good call I am doing 2 things at once

8. 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. Seems like I should get some sleep missing stupid things

10. 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. 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)

12. 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. Post what your actual code looks like now, from that, that isn't possible

14. 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. 0 to 11 = 12, 0 to 10 = 11

Page 2 of 3 First 123 Last
Popular pages Recent additions