Thread: help with modulus in C

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    5

    help with modulus in C

    Hi,
    I'm struggling to understand the modulus operator and why it isn't returning what I expect it to return in an equation.

    Using the below equation, when I have y down as 420 I get b = 2, however when compiling it within a programme b = 1. Please can someone explain to me why this happens?

    b = (y % 25) / 10

    Thank you,
    Salome

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,378
    No idea. I get 2.
    If you're getting 1, post your test case.
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    int main(void)
    {
      int y = 420;
      int b = (y % 25) / 10;
      printf("b=%d\n",b);
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    b=2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    5
    Quote Originally Posted by Salem View Post
    No idea. I get 2.
    If you're getting 1, post your test case.
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    int main(void)
    {
      int y = 420;
      int b = (y % 25) / 10;
      printf("b=%d\n",b);
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    b=2
    my output wasn't exactly what I wanted so I have narrowed the problem down to 'b' so changed the code so that only 'b' would be printed rather than the actual answer to the problem. I just need to know why I'm getting 1 in my programme for 'b' rather than 2. Thank you for your help.

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    
    float get_positive_float(string prompt);
        
    int main(void)
    {
        float change_required = get_positive_float("How much change? ");
        int y = change_required * 100;
        int a, b, c, d, e;
            
        a = y / 25;
        b = (y % 25) / 10; 
        c = (y - (a * 25) - (b * 10)) / 5;
        d = y - (a * 25) - (b * 10) - (c * 5);
        e = a + b + c + d;
        
        if (y > 0)
        {
            printf("%d\n", b);
        }
    }
    
    
    float get_positive_float(string prompt)
    {
        float change_required;
        do
        {
            change_required = get_float("%s", prompt);
        }
        while (change_required <= 0);
        return change_required;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,378
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    int main(void)
    {
      int y = 419;  //!! NOT 420
      int b = (y % 25) / 10;
      printf("b=%d\n",b);
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    b=1
    > int y = change_required * 100;
    Floating point numbers are pesky things at times.
    You might think you typed in 4.2, and reasonably expect y to be 420.
    But I bet you'll find it's really 4.199999999999999 and you have y=419 as a result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    5
    Of course...thank you. So straight forward, but yet not!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modulus
    By Caiz in forum C Programming
    Replies: 2
    Last Post: 04-26-2017, 03:08 PM
  2. Modulus
    By Caiz in forum C Programming
    Replies: 3
    Last Post: 04-24-2017, 03:44 AM
  3. %(Modulus)
    By cuo741 in forum C Programming
    Replies: 19
    Last Post: 05-14-2010, 08:50 AM
  4. modulus
    By nicknack in forum C Programming
    Replies: 7
    Last Post: 12-15-2007, 09:45 AM
  5. Help with Modulus
    By boontune in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 10:26 AM

Tags for this Thread