problem with this expression: (n%k)/k

This is a discussion on problem with this expression: (n%k)/k within the C Programming forums, part of the General Programming Boards category; hello everybody! I wanted to do an exercise which ask to write a function which receives in input an int ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    problem with this expression: (n%k)/k

    hello everybody!
    I wanted to do an exercise which ask to write a function which receives in input an int and extracts every digit from it( ex: 124--> 1 2 4).
    here is my code. i cannot understand why (n%k)/k outputs always 0 (which is false!)..
    thanks for your help!

    PS: May you suggest me other ways of dealing with digits (eg. if I 'd like to reverse a number 123 in 321 etc)? thanks again!

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void digitSeparator (int);
    
    int main()
    {
     int n;
     printf("Insert a number: "); scanf ("%d",&n);
     digitSeparator(n);
    }
    
    void digitSeparator (int n)
    {
    
     int k=1, exit=0;
    
     while (exit==0)
     {
       if ((int)(n/k)==0)exit=1;
       if (n%k!=0) printf("%d\n",(n%k)/k);
       k*=10;
     }
    
    }
    The output i get is:

    Code:
    gcc -lm eserc5_22.c -o eserc5_22
    f@f-laptop:~exercises$ ./eserc5_22
    Insert a number: 123
    0
    0
    0
    Last edited by smoking81; 01-30-2008 at 09:39 AM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    n&#37;k will always be < k, thus dividing the result by k will be < 1, and thus rounded to 0.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, for integer values, I would expect (n%k)/k to always be zero - since for non-zero K, (n%k) is always less than k, and an integer division of a/b where b > a and b != 0, the result is zero.

    --
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    http://cpwiki.sf.net/User:Elysia/Indentation
    You badly need to learn to indent properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    Wink

    i cannot understand this.. My reasoning is what follows:

    if i use n%k i get something like this:
    Code:
    Insert a number: 123
    3
    23
    123
    what i want is "3 2 1" and this is exactly (int)3/1, (int)23/10=2, (int)123/100=1.This sequence 1, 10, 100,.. is exactly k..
    what is wrong in my mind?
    can you give me some hints to overcome this problem? thanks a lot!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you shouldn't divide by k in the same line?

    --
    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.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by matsp View Post
    Perhaps you shouldn't divide by k in the same line?

    --
    Mats
    no i tried already all these things...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    no i tried already all these things...
    What did you try along the lines of not dividing by k in the same statement?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by laserlight View Post
    What did you try along the lines of not dividing by k in the same statement?
    i tried also this
    Code:
    void digitSeparator (int n)
    {
    
      int k=1, exit=0;
    
      while (exit==0)
      {
      if ((int)(n/k)==0)exit=1;
    
      if (n%k!=0) printf("%d ",(n%k));
      int a=(n%k); printf("%d\n",(a/k));
    
      k*=10;
      }
    }
    and the output is:
    Code:
    Insert a number: 346
    0
    6 0
    46 0
    346 0

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I think you have the rough idea, but are struggling to express it in code. Compile and run this example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = 346;
        printf("&#37;d\n", n % 10);
        n /= 10;
        printf("%d\n", n % 10);
        return 0;
    }
    Based on what you conclude from this, correct your own code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by laserlight View Post
    I think you have the rough idea, but are struggling to express it in code. Compile and run this example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = 346;
        printf("%d\n", n % 10);
        n /= 10;
        printf("%d\n", n % 10);
        return 0;
    }
    Based on what you conclude from this, correct your own code.
    thank you very much laserlight! with your hint i wrote immediately another function "reverseDigits()" and then came back to my problem which I solved as follows:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void digitSeparator (int);
    
    int main()
    {
    
    int n;
    printf("Insert a number: "); scanf ("%d",&n);
    digitSeparator(n);
    
    }
    
    void digitSeparator (int n)
    {
    
      int digits = ceil(log10(n));
    	if (digits==0) digits++;
    
      int k=pow(10,digits-1);
      printf("K:%d\n",k);
    
      while (1)
      {
        if (k>0)
        {
          if (n/k==0) break;
          printf("%d ",(int)(n/k));
          
          n=n%k;
          if ((k/10)>0)k/=10;
          else break;
        }
    
    
      }
    
    printf("\n");
    
    }
    what do you think about this strategy?
    thanks again! Bye

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    what do you think about this strategy?
    It is still a little too complicated.

    By the way, you need to indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I consider this piece of code
    Code:
    void digitSeparator (int n)
    {
    
      int digits = ceil(log10(n));
    	if (digits==0) digits++;
    
      int k=pow(10,digits-1);
      printf("K:%d\n",k);
    
      while (1)
      {
        if (k>0)
        {
          if (n/k==0) break;
          printf("%d ",(int)(n/k));
          
          n=n%k;
          if ((k/10)>0)k/=10;
          else break;
        }
    
    
      }
    
    printf("\n");
    
    }
    rather hard to read for what it does. Try rearranging (restructuring/indenting) it.

    Also, it won't work for specific values (10^X where X > 0). In fact, it won't work for numbers whos last digits = 0. Example, 1240, 12400. Basically, your code, beside being illisible, is wrong!

    (Also, you should verify if n is positive at the beginning of your function.)

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Does anyone note how newbies always tend to ignore suggestions that's outside the topic of what they're trying to do, like not using scanf("&#37;s", ...) or don't use void main or indent properly?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

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