We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,439 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with a function

Hey guys! I'm having a problem and I hope you guys can help me figure it out. I have to create a program that prints all the armstrong numbers from a user given lower and upper bound. I have a lot of the code but I can't seem to get a specific function right. The function is supposed to basically seperate a given number. So if x = 153, the function should seperate it into the numbers 1, 5, and 3. Problem is I have to seperate them and pass them to another function without an array. Here's what I have so far. The function I'm having problems with is called DthDigitofX.

//-----------------------------------------------------
int main()
//-----------------------------------------------------
{
   boolean IsArmstrongNumber(int x);

   int LB,UB,x;

   printf("LB? "); scanf("%d",&LB);
   printf("UB? "); scanf("%d",&UB);

   for(x = LB; x <= UB; x++)
   {
        if(IsArmstrongNumber(x) == true)
             printf("%i is an Armstrong Number", x);    
   }

   system("pause");

   return( 0 );
}

//-----------------------------------------------------
boolean IsArmstrongNumber(int x)
//-----------------------------------------------------
{
   while (x >= 0)
   {
      int NumberOfDigits(int r);
      int DthDigitOfX(int x,int d);

      int sum,d,temp;
      int n = NumberOfDigits(x);

      for (d = 1; d <= n; d++)
      {
         sum = sum + pow(DthDigitOfX(x,d),n);
      }

      if(sum == x)
         return true;
      else
         return false;
    }
}

//-----------------------------------------------------
int NumberOfDigits(int x)
//-----------------------------------------------------
{

while(x >= 0)
{
   int r = 0;

   do
   {
      r++;
      x /= 10;
      //printf("%i\n", r);
   }
   while ( x != 0 );
   return( r );
}
}

//-----------------------------------------------------
int DthDigitOfX(int x,int d)
//-----------------------------------------------------
{
   while (x >= 0)
   {
      for (d = 1; d <= 10; d++)
           x = x % 10;

     return(x);
   }
}
4
Contributors
7
Replies
8 Hours
Discussion Span
8 Months Ago
Last Updated
8
Views
lscamaro
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hey,

"boolean" is not a data type, look at the correct definition/declartion

Also, are you defining the function prototypes before main (since you're using them after main) because if not, the program won't know where to call the fucntions.

Hope this helps :)

phorce
Master Poster
751 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

The function I'm having problems with is called DthDigitofX.

Problems mean nothing to me. Describe exactly WHAT is the problem?

iamthwee
Posting Genius
6,378 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 489
Skill Endorsements: 35

The problem is that it correctly extracts the first number but doesn't do the rest. For example, if I have 153, it will extract the 3 but not the rest.

lscamaro
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Well just concentrate on this first.

Create one function which spits out all the numbers, 3 ,5,1

put a load of couts and see what you come up with.

(You may want to consider dividing by multiples of ten as well as using the modulus operator as a hint)

Also phorce makes a point boolean is not a datatype I think bool is though. But concentrate on the above.

iamthwee
Posting Genius
6,378 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 489
Skill Endorsements: 35

Okay, using your advice, I think my problem is more how to pass the individual numbers from function to function than how to seperate them. Correct me if I'm wrong but this should seperate them correctly?

 int a = 0;
 for (d = 1; d <= 10; d++)
      {
           a = x % 10;
           x = x / 10;
      }
lscamaro
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Correct me if I'm wrong but this should seperate them correctly?

Check it with a few examples? Do most of them work, if so you've answered your question. The loop looks a little dubious though, as long as d <=10, I wonder if the integer you passed in was more than ten digits what would happen? OK you can't declare ints that big but best to make your code extra safe.

I think my problem is more how to pass the individual numbers from function to function than how to seperate them

If this is the case then try writing the program without functions. People tend to see the logic much clearer so they know exactly where they're going wrong and what the function must do and where they must place it.

iamthwee
Posting Genius
6,378 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 489
Skill Endorsements: 35

Why are you running the loop till d <= 10 ? Can't understand this part.

Anyways try this, see if it works or not.

int DthDigitOfX( int x, int d)
{
    int ans = 0 ;

    for( int i = 0 ; i < d ; i++)
    {
        ans = x % 10;
        x = x / 10;
    }

    return ans;
}

What I did is, ran the loop till the dth digit. Not 10. It will give the digit from last, like if its 12345, 2nd digit would be 4, 4th digit would be 2.

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0808 seconds using 2.75MB