# Adding integers inside of an array

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 11-09-2012
Sorinx
Haha well this way would be a lot easier and outside the box for him
• 11-09-2012
randyjohnson
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?
• 11-09-2012
Sorinx
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
• 11-09-2012
randyjohnson
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
• 11-09-2012
Sorinx
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
• 11-09-2012
camel-man
FYI SumEven and SumOdd should be initialized to 0 in the beginning so you are not left with some outrageously high number.
• 11-09-2012
Sorinx
Yeah good call I am doing 2 things at once
• 11-09-2012
camel-man
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];```
• 11-09-2012
Sorinx
Seems like I should get some sleep missing stupid things
• 11-09-2012
randyjohnson
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-09-2012
Sorinx
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)
• 11-09-2012
randyjohnson
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)`
• 11-09-2012
Sorinx
Post what your actual code looks like now, from that, that isn't possible
• 11-09-2012
camel-man
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.
• 11-09-2012
Sorinx
0 to 11 = 12, 0 to 10 = 11
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last