Like Tree3Likes

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; I'm horrible at C and I have to write a program for class that's due tomorrow that has to do ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    23

    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. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    for(i=0;i<arraysize; i+=2)
    sumEven+=array[i];
    sumOdd+=array[i+1];

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    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.
    Last edited by camel-man; 11-09-2012 at 08:19 PM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    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. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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
    Last edited by Sorinx; 11-09-2012 at 08:33 PM.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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
    randyjohnson likes this.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    @ 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.
    Last edited by camel-man; 11-09-2012 at 08:45 PM.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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. #12
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    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. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by camel-man View Post
    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. #15
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    No, I know how to "move the pointer inside of the cell" just fine :P

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 11:50 AM
  2. Adding Long Integers!
    By alireza beygi in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 07:11 AM
  3. adding 16bit and 32 bit integers
    By kris_perry2k in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 09:49 AM
  4. Adding integers to a string
    By Kyoto Oshiro in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 08:01 AM
  5. C++ help!!!!! Adding integers
    By PR1MO in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21