Hello again everyone. I've been trying to finish this code that calculates the eigen digit (i.e. 2349 is 2+3+4+9=18=1+8=9. It wont give me a single answer though. And we are supposed to do it through a recursive function. The code looks like this:

#include <iostream>
using namespace std;
int calcDigit1(int,int);
int main(){
int n;
	cout<<"input a number greater than zero:"<<endl;
	cin>>n;
	cout<<"The eigen digit of this numberis:"<<calcDigit1(n,1)<<endl;
}
int calcDigit1(int n,int a){
	if(n<=0)
		return a;
	return calcDigit1(n%10,a+n);
}

I dont understand what is wrong with it, so your help is greatly appreciated. And please keep in mind that i am a complete beginner at this.

This is the way I would approach it:
Call a function Eigen from main.
Have an array of ints in eigen. Store the first number in array[0], second in array[1] and so on.

For example, with the number 2349, store 2 in array[0], 3 in array[1], 4 in array[2], 9 in array[3]. Then, write a loop to add each element of the array, and store that in a variable eigen_Val. After you do this, call Eigen(eigen_val).

I will work on some code in a few minutes to put up here.

int main()
{
int n;
cout << "input number" << endl;
cin >> n;
Eigen(n);

return 0;
}

void Eigen(int n)
{
int array[];
//dynamically allocate array here, i don't feel like writing the code for that
int var; //holds array size
int length, temp;
temp = n;
length = 0;
if(n < 10)
  {
   cout << "Eigen value is " << n << endl;
   exit(1);
  }
else
  {
    //find the length of n
     while(temp > 1)
     {
     temp = temp/10;
     length += 1; 
     }

     for(int i=1; i <= length; i++)
     {
     array[i-1] = n % (10 ^ i);
     }

     for(i = 0; i < var; i++)
     {
          n+=array[i];
     }
  }
free(array);
Eigen(n);
}

ok it gives me an answer now, however, the answer is always whatever was inputed to n.
Basically, i put it n as 2349, it gives back 2349, if n=23, then the answer comes as 23.
It looks like this now:

#include <iostream>
using namespace std;
int calcDigit(int,int);
void main(){
int n;
	cout<<"input a number greater than zero:"<<endl;
	cin>>n;
	cout<<"The eigen digit of this number is:";
	cout<<calcDigit(n,0)<<endl;
}
int calcDigit(int n,int a){
	if(n<10)
		return a;
	return calcDigit(n%10,a+n);
}

I forgot to say this at first, but someone managed to finish this in many,many less lines than the ones I have.

I still cannot figure out what is wrong with my code, could i get some help please? I really need to finish this today by 10 am.

I'd declare a function accepting an int and returning void.

The stopping value would be when the int passed in is less than 10, since that would be
Eigen digit

If the int passed in wasn't the Eigen digit, then I'd declare an int called temp to pass to the next call of the funciton and I'd determine the individual digits of the int passed in by sequentially passing the int passed in through a loop, decreasing the value of the int passed in by dividing by 10 each time through the loop and continuing as long as the modified value of the int passed in was greater than zero. Within the loop I'd get the right hand digit of the current value of the int passed in by using modulo operator (% 10) and adding it to temp, before dividing the current value by 10. When the loop finished I'd recursively call the function, passing it temp.

Okay I made this code on Turbo C

#include<iostream.h>

int eig(int num)
{

if(num<10)
{
return num;
}
else
{
return eig(num/10)+(num%10);
}

}

void main()
{
int n,ans=0;
cin>>n;
ans=eig(n);
while(1)
{
if(ans<10)
{
break;
}
else
{
ans=eig(ans);
}
}
cout<<"Eigen number is "<<ans<<endl;

}

Its not totally recursive though. I will post if can make a totally recursive one.

Thank you so much for your help. I didn't think it was allowed to put the calculations for the eigen number as a prototype. I was taught that all calculations had to go below MAIN.

Okay here is another shorter alternative.

#include<iostream.h>

int eig(int num)
{

if(num<10)
{
return num;
}
else
{
return eig(eig(num/10)+(num%10));
}

}

void main()
{
int n,ans=0;
cin>>n;
ans=eig(n);
cout<<"Eigen number is "<<ans<<endl;

}
commented: !! +6

hammerhead's use of the function definition before main() is valid, though using a function prototype before main() and a function definition after main() is valid as well.

However, don't adopt his use of void as the return type for main().

In addition, this line is suspect in my opinion.

return eig(eig(num/10)+(num%10));

though I don't have compiler to run it at this time.

Here's pseudocode for my initial post.

calcEigD(n)
  if n < 10 
     output n as Eigen digit
  else
    temp
    while n > 0
       temp += n % 10
       n /= 10
    calcEigD(temp)

This needs fleshing out. Copy and pasting won't work. Be sure you understand what each line is doing so you can flesh it out appropriately and defend it if asked.

From the test cases I used, the code was working fine (till the max value of int) . I usually write down a mathematical expression before coding it. The function for sum of the digits of a whole number would be

f(x)   =    {     x if x is single digit i.e x<10

                     f(x/10)+(f%10)  otherwise
               }

if the sum returned by this function is more than a single digit number, we can use the same function again to find the eigen number. That is what I have done in the first code by using a while loop.

If we use

f(f(x/10)+(x%10))

it will calculate the sum of sum of the digits unless the number or its sum comes out to be a single digit number. A few examples

f(1)= 1            as x <10
f(10)= f(f(1)+0)=f(1)=1
f(209)=f(f(20)+9)=f(f(2)+0+9)=f(2+9)=f(11)=f(1)+1=1+1=2

I think the function is correct but if there is some problem kindly inform me. It will be highly appreciated.

Oops correction
f(209)=f(f(20)+9)=f(f(f(2)+0)+9))=f(f(2)+9)=f(11)=f(1)+1=2

So many f's . But again I might be wrong again :) . I havnt tested it for values where sum of the digits is a number of three digits or more.

I follow your mathematical analysis. What I'm not so sure of is your C++ code. My concerns, perhaps unfounded, are 1) the use of two, nested calls to the same function from within the function and 2) passing a function as an arguement to a function (I've seen function pointers and function objects (functors) passed as arguements, but i don't remember seeing functions themselves passed as arguments).

If you've compiled and run the program and it works with a variety of input, then I've learned something today, which is never a bad thing!

int eig( int num )
{
  if( num<10 ) return num;
  else return eig( eig( num/10 ) + (num%10) ) ;
}

cool!
though you spoiled it by void main() and #include<iostream.h>

Functions accepting functions as parameters. Worked on my compiler with several different inputs. My hat is off to ya.

Functions accepting functions as parameters. Worked on my compiler with several different inputs. My hat is off to ya.

int eig( int num )
{
  if( num<10 ) return num;
  else return eig( eig( num/10 ) + (num%10) ) ;
}

I don't think it is function accepting function as parameters from above code. It is still accepting int. The code above just use the result of the function as input argument to itself. In fact above code is a recursive function.

A function accepting function as parameter would be declared like int eig(int (*fn)(int)) where fn is a variable that point to a function that take in a int argument and return a int result.

Thank you for that clarification. Gee, I've learned something new today, too!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.