Haha well this way would be a lot easier and outside the box for him
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...
Haha well this way would be a lot easier and outside the box for him
Okay this is what I have set up so farDo I have it placed all in the correct order?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;
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.
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 loopsCode:#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; }
Last edited by Sorinx; 11-09-2012 at 08:12 PM.
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
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
FYI SumEven and SumOdd should be initialized to 0 in the beginning so you are not left with some outrageously high number.
Yeah good call I am doing 2 things at once
Not to nitpick here but also you want to switch the indexes of sumEven and Sumodd
switch toCode:sumEven+=a[i];//<---- this is first number of array meaning 1 in reality. 1= odd sumOdd+=a[i+1];// <--- second number 2= even :D
Code:sumOdd+=a[i]; sumEven+=a[i+1];
Seems like I should get some sleep missing stupid things
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?
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)
Last edited by Sorinx; 11-09-2012 at 08:53 PM.
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 thisCode:for(i=0;i<11; i+=2)
Post what your actual code looks like now, from that, that isn't possible
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.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; }
0 to 11 = 12, 0 to 10 = 11