creating nd calling function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hassaan299
    New Member
    • Sep 2015
    • 2

    creating nd calling function

    while working on a module i came across a problem and could not figure out the solution to the problem. here is the question . program will get the input number and position from the user output the digit which is is at that position. for example if number = 256314 and position = 0 then output should be "2" if number = 1256985 and position = 3 then output should be "6" here is my code
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int returnDigitAtPosition(int &position,int &totaldigits,int &number)
    {
    
        {   
             int positiondigit = number/pow(10,(totaldigits - position));
             positiondigit = positiondigit % 10;
                return positiondigit;
        }
    }  
     int main()
    {
        int number = 0,position = 0,digit = 0;
        cout << " Enter the number :  ";
        cin>>number;
        cout<<" Enter the position : ";
        cin>>position;
        /*cout<<" Enter the digit : ";
        cin>>digit;*/
        int totaldigits = log(number);
        int DigitAtPosition =  returnDigitAtPosition(position,totaldigits,number);
        cout<<DigitAtPosition;
    
    
    
    
    
    }
  • hassaan299
    New Member
    • Sep 2015
    • 2

    #2
    you should be using log10(number) not log(number)

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Your integer does not contain 1256985.

      It contains 000100110010111 000011001.

      Maybe you are thinking of an array of char?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Your code counts on implicit type conversions -- some of which are a little subtle.
        Code:
        int number;
        int totaldigits = log10(number);
        log10 expects its argument to be a double, but you pass an int. That's ok, the implicit conversion rules do what you want; and probably nobody is surprised.
        You assign the double returned by log10 to an int. This is potentially a problem -- will the implicit conversion truncate, round-to-nearest, or round-up? The rule is to truncate. Is that the behavior you need?
        Code:
        int positiondigit = number/pow(10,(totaldigits - position));
        pow expects two doubles but you pass it two ints. We already know that will be ok.
        pow returns a double so the division will be performed using floating-point math; and that floating-point result is assigned to an int. As pointed out above, the implicit conversion will truncate. Is that the behavior you need?

        I don't mean to suggest these double-to-int conversions will necessarily mess you up. I just want you to be aware of the assumptions you are making. If you know the compiler does what you want then great; but if you're not sure then you should either find out or change your program so that it doesn't depend on those assumptions. It might be worthwhile to add comments to remind yourself (and the next person) that you verified the behavior is as expected.

        What does your program do if number is 0?
        Do you need to support when number is negative? What should the output be when number is negative?

        What does your program do if the user enters an invalid position (negative or bigger than the number of digits)?

        Warning: I described the implicit conversion rules for C. I assume they are the same for C++, but I don't really know.

        Comment

        Working...