Function

This is a discussion on Function within the C Programming forums, part of the General Programming Boards category; This program reads an integer n of M digits (defined at the beginning) and a one-digit integer d; if d ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    Function

    This program reads an integer n of M digits (defined at the beginning) and a one-digit integer d; if d is in n it has to print n and and the sum (+) symbol below every occurance of d.
    It compiles, and works fine in the case M=1,2 and 3, but it starts doing funny things when M=4 and up...

    Any advice is appreciated.
    Thanks!



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define M 4
    
    int procesar(int n, int d);
    
    int procesar(int n, int d)
    {
    int esta=0;
    int i,aux;
    
    for(i=0;i<M;i++)
        {
        aux=n/pow(10,M-(i+1));
        if(aux==d)
            {
            esta=1;
            printf("+");
            }
        else
            printf(" ");
        n=n-(aux*pow(10,M-(i+1)));    
        }
    return esta;
    }
    
    int main()
    {
    int n,d;
    
    scanf("%d %d",&n,&d);  
    printf("%d\n",procesar(n,d));
    
      system("PAUSE");	
      return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help quite a bit if you describe the difference betwen what you expect and what you actually get (what is "funny").

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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    you do not need pow to extrct digits from number
    enough % and /
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Quote Originally Posted by vart View Post
    you do not need pow to extrct digits from number
    enough % and /
    Ok, but it should work too that way.

    In the case M=4, this is what happens:

    Code:
    1234 4
        0
    or

    Code:
    1244 4
      + 1

    Im aware that the "0" and "1" that appears is the return that is in the program (which by the way I dont know how to get rid of...) but the last 4 shold have a "+" below...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that your float to integer calculation gets rounding errors, and you are not getting the results you expect.

    I added a printf to show the value of n after the "n = n - ..", and it shows this:
    Code:
    1234 4
     n = 233
     n = 33
     n = 3
     n = 0
    0
    I then modified the code to store the result of pow() and it changes the output... So you are definitely suffering from some sort of rounding problems.

    Using / and % operators is a much safer method for this type of math.

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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Surely we've figured out what printf is by now.... If you don't want something to print, don't printf it.

    And your example is incorrect: input of 1234 4 gives
    Code:
    1234 4
       +1
    which is what you wanted, so I'm not sure what your problem is.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Surely we've figured out what printf is by now.... If you don't want something to print, don't printf it.

    And your example is incorrect: input of 1234 4 gives
    Code:
    1234 4
       +1
    which is what you wanted, so I'm not sure what your problem is.
    Actually, I can reproduce the exact results of the original poster - however, I think it may well depend on the compiler & settings. gcc-mingw (3.4.x) that is distributed with dev-cpp seems to reproduce the problem, as stated above - that is, with no extra options - adding -O3, and it "works", but -O1 -ffast-math doesn't, for example.

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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by matsp View Post
    Actually, I can reproduce the exact results of the original poster - however, I think it may well depend on the compiler & settings. gcc-mingw (3.4.x) that is distributed with dev-cpp seems to reproduce the problem, as stated above - that is, with no extra options - adding -O3, and it "works", but -O1 -ffast-math doesn't, for example.

    --
    Mats
    Hey, you're right. (I have -O3 set in my IDE, which is why it didn't occur to me to go without. Take it away and bad things happen.)

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    matsp:


    Could you give me an example of how it should be with % and /??

    So, it is wrong or what?? Now Im getting confused....

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Using pow for extracting digits is like using a sledgehammer to swat a fly. Whilst initially that only seems like overkill, it is so much so that it's actually become the opposite in this case. (You'd fail to kill flies with it very effectively at all).

    Or if you prefer a different metaphor, you're walking around the block to get next door.

    I'm sure you get the picture. Anyway to use / and %:
    Using % N on a positive number will give you the remainder if you divided that number by 10. So for example 2368 % 10 gives you 8.
    Now, can you spot what operation you can perform on 2368 to move the digits along by one digit, such that you can use % 10 again to get out the 6?
    Now, keep doing those two things until... you have zero left.

    Simple huh!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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