Modulus Operator!

This is a discussion on Modulus Operator! within the C Programming forums, part of the General Programming Boards category; I'm trying to Down size a 5 digit number on an array of [26] values each value has well 5 ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Modulus Operator!

    I'm trying to Down size a 5 digit number on an array of [26] values each value has well 5 digits
    i want to just use the last two digits for the value how would i be able to do that with
    the modulus operator. I was told the mod OP would be perfect, i just dont know how to use it with arrays..

    thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Um..... I think you want to do a modulus operation of 100 on each element in the array, but your description is so ambiguous and vague, I can't figure out what you want.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    i did specify that i dont know how to use the modulus when it comes to arrays i get errors when i try to use my assignments.

    examples would be

    array[90] %= 100

    *(array % 100)
    ...........................
    lame examples but im new sorry

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    So what do you want? An array tutorial, or a modulus tutorial?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    unless you want to rewrite the C Primer book is up to you. But no really i would just like an example on how would i be able to use the modulus OP on an array with values but im guessing its hard to think of the answer.

    embeded systm

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You're guessing wrong. This is super easy.

    Code:
    x = array[0] % 100;
    Assuming all variables are ints in this example, if array[0] is 1234, x should contain 34.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, this would work: array[90] %= 100; (if you want to assign the result back to array[90])

    What errors do you get? What does your code look like?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And it's worth noting that the language C doesn't have ANY operations that operate on all elements in an array - you can call a function that, given the array and the size of it, can do something on the whole array by ways of doing a loop in the function, but the C language itself only sees arrays as a sequence of elements - there's no way you can actually operate on "all elements of this array" in one fell swoop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. modulus operator / '%' question
    By musikluvah in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 11:09 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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